Author Topic: A problem for calling WINAPI CreateProcessWithLogonW~  (Read 2773 times)

Offline vcvycy

  • Single posting newcomer
  • *
  • Posts: 4
A problem for calling WINAPI CreateProcessWithLogonW~
« on: September 02, 2016, 06:07:46 am »
/* 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"[email protected]";
  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
                    );

   

Offline osdt

  • Multiple posting newcomer
  • *
  • Posts: 61
Re: A problem for calling WINAPI CreateProcessWithLogonW~
« Reply #1 on: September 02, 2016, 07:29:16 pm »
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.

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

Offline vcvycy

  • Single posting newcomer
  • *
  • Posts: 4
Re: A problem for calling WINAPI CreateProcessWithLogonW~
« Reply #2 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)) ===|


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?

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7012
    • My Best Post
Re: A problem for calling WINAPI CreateProcessWithLogonW~
« Reply #3 on: September 03, 2016, 05:28:59 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)) ===|

Yes, did it wrong.

From memory you need to add it like below; the CB defines are one per line.
Code: [Select]
_WIN32_WINNT=0x0500

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?

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

Edit: Fixed typos.

Tim S.
« Last Edit: September 03, 2016, 05:30:50 am by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 32 bit.
On Debian Stretch, compiling CB Trunk against wxWidgets 3.0.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline vcvycy

  • Single posting newcomer
  • *
  • Posts: 4
Re: A problem for calling WINAPI CreateProcessWithLogonW~
« Reply #4 on: September 03, 2016, 07:46:45 am »
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?