User forums > Help

A problem for calling WINAPI CreateProcessWithLogonW~

(1/1)

vcvycy:
/* Sorry For My Poor English =.=*/
 The code below can run successfully in vs2012。
But when I compiled it in Codeblocks, Codeblocks always reported an error:
      error: 'CreateProcessWithLogonW' was not declared in this scope|

 It puzzled me~
 #include<windows.h>
  BOOL bCreateProc =CreateProcessWithLogonW (
                      wcUserName,
                      NULL,
                      wcPassword,
                      0,
                      NULL,
                      wcCommandLine,
                      NORMAL_PRIORITY_CLASS,
                      NULL,
                      NULL,
                      &si,
                      &pi
                    );
   Then I maked a test:
   use LoadLibray("Advapi32.dll") and GetProcName("CreateProcessWithLogonW")to call this api.
   It Works!!!!!!!!!!!
   So Why this happend?????
   Code Like This:
HMODULE hDll=LoadLibrary("Advapi32.dll");
  FARPROC p=GetProcAddress(hDll,"CreateProcessWithLogonW");
  printf("%x\n",p);
  BOOL WINAPI (*pfun)(
  _In_        LPCWSTR               lpUsername,
  _In_opt_    LPCWSTR               lpDomain,
  _In_        LPCWSTR               lpPassword,
  _In_        DWORD                 dwLogonFlags,
  _In_opt_    LPCWSTR               lpApplicationName,
  _Inout_opt_ LPWSTR                lpCommandLine,
  _In_        DWORD                 dwCreationFlags,
  _In_opt_    LPVOID                lpEnvironment,
  _In_opt_    LPCWSTR               lpCurrentDirectory,
  _In_        LPSTARTUPINFOW        lpStartupInfo,
  _Out_       LPPROCESS_INFORMATION lpProcessInfo
);
  pfun=(BOOL WINAPI (*)(
  _In_        LPCWSTR               lpUsername,
  _In_opt_    LPCWSTR               lpDomain,
  _In_        LPCWSTR               lpPassword,
  _In_        DWORD                 dwLogonFlags,
  _In_opt_    LPCWSTR               lpApplicationName,
  _Inout_opt_ LPWSTR                lpCommandLine,
  _In_        DWORD                 dwCreationFlags,
  _In_opt_    LPVOID                lpEnvironment,
  _In_opt_    LPCWSTR               lpCurrentDirectory,
  _In_        LPSTARTUPINFOW        lpStartupInfo,
  _Out_       LPPROCESS_INFORMATION lpProcessInfo
 ))p;

  STARTUPINFO si={sizeof(si)};
  wchar_t name[100]=L"352871242@qq.com";
  wchar_t pass[100]=L"pass";
  wchar_t cmd[100] =L"notepad";
  PROCESS_INFORMATION pi;
   BOOL bCreateProc =(*pfun) (
                      name,
                      NULL,
                      pass,
                      0,
                      NULL,
                      cmd,
                      NORMAL_PRIORITY_CLASS,
                      NULL,
                      NULL,
                      (LPSTARTUPINFOW)&si,
                      &pi
                    );
   

osdt:

--- Quote from: MSDN ---https://msdn.microsoft.com/en-us/library/windows/desktop/ms682431(v=vs.85).aspx

To compile an application that uses this function, define _WIN32_WINNT as 0x0500 or later.

--- End quote ---

AFAIK VS does it automatically, for CB use Project->Build Options->#defines

vcvycy:
When I added "#define _WIN32_WINNT 0x0500" in "Build Options->Compiler settings->#defines",
It report
||=== Build: Debug in Test (compiler: GNU GCC Compiler) ===|
 _WIN32_WINNT||No such file or directory|
 0x0500||No such file or directory|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

When I added  "#define _WIN32_WINNT 0x0500" in main.cpp file,I seems nothing hanppend,still reported that function CreateProcessWithLogonW was not declared in the scope.

Do I did something wrong?

stahta01:

--- Quote from: vcvycy on September 03, 2016, 05:15:53 am ---When I added "#define _WIN32_WINNT 0x0500" in "Build Options->Compiler settings->#defines",
It report
||=== Build: Debug in Test (compiler: GNU GCC Compiler) ===|
 _WIN32_WINNT||No such file or directory|
 0x0500||No such file or directory|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

--- End quote ---
Yes, did it wrong.

From memory you need to add it like below; the CB defines are one per line.

--- Code: ---_WIN32_WINNT=0x0500

--- End code ---


--- Quote from: vcvycy on September 03, 2016, 05:15:53 am ---When I added  "#define _WIN32_WINNT 0x0500" in main.cpp file,I seems nothing hanppend,still reported that function CreateProcessWithLogonW was not declared in the scope.

Do I did something wrong?

--- End quote ---

You need to add the defines before any windows headers are included.

Edit: Fixed typos.

Tim S.

vcvycy:
WOW!Problem Solved.
Just do as you say, Modify "#define _WIN32_WINNT 0x0500" to "_WIN32_WINNT=0x0500".
thanks!
When I want to use WinSock API ,I have to add "-lwsock32" in "Linker Settings",I perfer CB to VS because its convience.
But why there is so much constarin in windows development?

Navigation

[0] Message Index

Go to full version