Autor Beitrag
tomycat
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 265
Erhaltene Danke: 1



BeitragVerfasst: Fr 28.10.16 11:09 
hallo,
ich lese gerade ein Buch und möchte folgenden Code ausprobieren:



ausblenden volle Höhe Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
#include "stdafx.h"
class CMainApp
{
public:
    static
    HRESULT
    MainHR(VOID)
    {
        HANDLE hProcess, hPrimaryThread;
        CHandle shProcess, shPrimaryThread;
        CHandle shWorkerJob;
        DWORD dwExitCode;
        JOBOBJECT_EXTENDED_LIMIT_INFORMATION exLimitInfo = {0};
        CStringW shCommandLine = L"notepad.exe";
        ChkProlog();
        //
        // Create the job object, set its processes to terminate on
        // handle close (similar to an explicit call to TerminateJobObject),
        // and then add the current process to the job.
        //
        shWorkerJob.Attach(CreateJobObject(NULL, NULL));
        ChkWin32(shWorkerJob);

        exLimitInfo.BasicLimitInformation.LimitFlags =
            JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;

        ChkWin32(SetInformationJobObject(
            shWorkerJob,
            JobObjectExtendedLimitInformation,
            &exLimitInfo,
            sizeof(exLimitInfo)
            ));

        ChkWin32(AssignProcessToJobObject(
            shWorkerJob,
            ::GetCurrentProcess()
            ));

        //
        // Now launch the new child process (job membership is inherited by default)
        //
        wprintf(L"Launching child process (notepad.exe) ...\n");
        ChkHr(LaunchProcess(
            shCommandLine.GetBuffer(),
            0,
            &hProcess,
            &hPrimaryThread
            ));
        shProcess.Attach(hProcess);
        shPrimaryThread.Attach(hPrimaryThread);

        //
        // Wait for the worker process to exit
        //
        switch (WaitForSingleObject(shProcess, INFINITE))
        {
            case WAIT_OBJECT_0:
                ChkWin32(::GetExitCodeProcess(shProcess, &dwExitCode));
                wprintf(L"Child process exited with exit code %d.\n", dwExitCode);
                break;

            default:
                ChkReturn(E_FAIL);
        }

        ChkNoCleanup();
    }

private: static HRESULT LaunchProcess(
        __in LPWSTR pwszCommandLine,
        __in DWORD dwCreationFlags,
        __out HANDLE* phProcess,
        __out HANDLE* phPrimaryThread
        )
    {
        STARTUPINFO si = {0};
        PROCESS_INFORMATION pi = {0};

        ChkProlog();

        ASSERT(pwszCommandLine);
        ASSERT(phProcess);
        ASSERT(phPrimaryThread);

        si.cb = sizeof(si);
        ChkWin32(CreateProcess(
            NULL,
            pwszCommandLine,
            NULL,
            NULL,
            FALSE,
            dwCreationFlags,
            NULL,
            NULL,
            &si,
            &pi
            ));

        *phPrimaryThread = pi.hThread;
        *phProcess = pi.hProcess;

        ChkNoCleanup();
    }
};

int __cdecl wmain(VOID)
{
    HRESULT hr;
    hr = CMainApp::MainHR();
    if (FAILED(hr))
    {
        wprintf(L"HRESULT: 0x%08X\n", hr);
    }
    else
    {
        wprintf(L"Success.\n");
    }
    return 0;
}


VS 2015 gestartet -> neue Projekt -> C++ konsolenanwendung
Alles gelöscht und den Code von oben 1 zu 1 hinzugefügt und anschließen den Rumpf dazugeschrieben (die main):


ausblenden Quelltext
1:
2:
3:
4:
5:
6:
  
int main()
{
  CMainApp();
    return 0;
}


Leider macht vs in Zeile 9 Rot:
ausblenden Quelltext
1:
2:
3:
4:
5:
  
static 
HRESULT   //<------- Der Bezeichner HRESULT ist nicht definiert
 MainHR
(VOID)  //<------- Der Bezeichner VOID ist nicht definiert


Moderiert von user profile iconChristian S.: cpp- durch code-Tags
Moderiert von user profile iconChristian S.: Topic aus Andere .NET-Sprachen verschoben am Fr 28.10.2016 um 11:18
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Fr 28.10.16 12:20 
Zwei Fehler:
Zum einen fehlt
ausblenden Quelltext
1:
#include <windows.h>					

Und zum zweiten ist die Hauptfunktion schon in deinem ersten Code als wmain deklariert (du mußt dazu allerdings UNICODE in deinen Projekteinstellungen aktiviert haben - ansonsten ändere diese einfach zu main).
tomycat Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 265
Erhaltene Danke: 1



BeitragVerfasst: Fr 28.10.16 12:48 
ok thx,

Unicode aktivieren? blöde Frage, nach was muss ich googeln?

Nach #include <windows.h> habe ich weniger rot :-)

In Zeile 18:
ausblenden Quelltext
1:
CHandle shProcess, shPrimaryThread; // CHandle <--- Der Bezeichner CHandle ist nicht definiert.					

Wenn ich dich richtig verstehe brauche ich keine zusätzliche main()?

Moderiert von user profile iconTh69: Code-Tags hinzugefügt
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Fr 28.10.16 14:28 
Hast du überhaupt ein MFC-Projekt angelegt? CHandle, CString, C... sind alles MFC-Klassen.
Dort in den Projekteinstellungen als Define "_UNICODE" angeben bzw. das Character-Set auf "UNICODE" setzen, s.a. How do I turn off Unicode in a VC++ project? (gleiches gilt für "turn on")
ssb-blume
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 375
Erhaltene Danke: 7

XP, W7, W8
Deutschland
BeitragVerfasst: Sa 29.10.16 09:26 
Hallo

ich habe die Erfahrung gemacht, das Code aus einem Buch oder auch anderen Veröffentlichungen (Zeitung..) immer einen kleinen Haken hat. Es fehlt entweder eine Angabe oder ein Zahlendreher..
Warum das so ist , keine Ahnung. Auch einigen Foren (NICHT DIESE!!) machen es so.
Ich hatte mal ein Problem mit FFT, ca. 25 Angaben gefunden, erst ein Chinese hatte ein auf Anhieb funktionierenden Code gepostet.

Hansi

_________________
Brain: an apparatus with which we think we think.