Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to use the CreateProcessWithLogonW function

Posted on 2002-07-10
8
Medium Priority
?
2,221 Views
Last Modified: 2010-04-04
Hello guys! Here is all my code
Why the execution of the CreateProcessWithLogonW may not work?



program Launch;
{$APPTYPE CONSOLE}
uses
  SysUtils, Windows,
  Dialogs;


function CreateProcessWithLogonW(lpUsdeerName,lpDomain,lpPassword: LPCWSTR;
  dwLogonFlags:dword; lpApplicationName : LPCWSTR; lpCommandLine: LPWSTR; dwCreateionFlags:dword;
  lpEnvironment:Pointer; lpCurrentDirectory: LPCWSTR; lpstartupinfo:tstartupinfo;
  lpProcessInfo:TProcessInformation): BOOL; external 'C:\WINNT\system32\advapi32.dll';



const
  pProgramName = 'ClipBoarder.exe';
  pUserName    = 'Administrator';
  pUserDomain  = '';
  pUserPassword = 'pwd';
  pNullStr = '';

var
  StartInfo  : TStartupInfo;
  ProcInfo   : TProcessInformation;
  CreateOK   : Boolean;
  Count : Integer;

  ProgramName,
  UserName,
  UserDomain,
  UserPassword,
  NullStr : PWideChar;

begin
  ProgramName := StringToOleStr(pProgramName);
  UserName := StringToOleStr(pUserName);
  UserDomain := StringToOleStr(pUserDomain);
  UserPassword := StringToOleStr(pUserPassword);
  NullStr := StringToOleStr(pNullStr);


  FillChar(StartInfo,SizeOf(TStartupInfo),#0);
  FillChar(ProcInfo,SizeOf(TProcessInformation),#0);

  StartInfo.cb := SizeOf(TStartupInfo);
  StartInfo.dwFlags := startf_UseShowWindow;
  StartInfo.wShowWindow := 4;

 try
{   CreateOK := CreateProcess(pProgramName,nil, nil, nil,False,
                 CREATE_NEW_PROCESS_GROUP+NORMAL_PRIORITY_CLASS,
                 nil, nil, StartInfo, ProcInfo);
}
   CreateOK := CreateProcessWithLogonW(
              UserName,
              UserDomain,
              UserPassword,
              0,
              ProgramName,
              nil,
              CREATE_DEFAULT_ERROR_MODE or NORMAL_PRIORITY_CLASS,
              nil,
              nil,
              startinfo,
              procinfo);
   if not CreateOK then RaiseLastWin32Error;

  except
    on E: Exception do
    begin
      writeln(E.Message );
    end;
  end
end.
0
Comment
Question by:penich
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 20

Expert Comment

by:Madshi
ID: 7143360
Are you sure that you gave in the correct password and user name? And what is the exact error message, please?

Regards, Madshi.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 7143375
Have you tried giving in the full program path? Also better give it in as the command line parameter, leave the program name to nil. That works better according to my experience.

Is that domain information correct? An empty strings is quite strange. That does NOT qualify for the local machine, as far as I know!
0
 
LVL 8

Expert Comment

by:TOndrej
ID: 7143557
I think that the current user account also needs privileges to impersonate other accounts.
Anyway, exact error code/message might be useful. <g>
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:penich
ID: 7144121
Additional info

1. Login and Password are correct
2. I tried to pass the full programname parameter like:
'c:\temp\clipboarder.exe'
'c:\\temp\\clipboarder.exe'
'clipboarder.exe'
3. My PC name is 'windev-3' but i don't have a domain name at all - very simple office network
4. There was no any error message at all - I could not even launch GetLastError function because the Access Violation occurs derectly in the CreateProcessWithLogonW function. (something like 'Access Violation ... in ntdll ....' )
0
 
LVL 6

Expert Comment

by:DrDelphi
ID: 7144471
What O/S and service pack are you using? According to M$' knowledgebase, this API function can only be used in W2K and higher.


Good luck!!
0
 
LVL 6

Expert Comment

by:DrDelphi
ID: 7144513
What O/S and service pack are you using? According to M$' knowledgebase, this API function can only be used in W2K and higher.


Good luck!!
0
 

Author Comment

by:penich
ID: 7145018
....
win2k, SP2
0
 
LVL 20

Accepted Solution

by:
Madshi earned 1500 total points
ID: 7145330
You didn't say that you get an exception. Well, I checked it out. There were several errors in how you defined CreateProcessWithLogonW. You forgot stdcall, also startupInfo and processInformation needed "const" and "var". This one works on my PC:

function CreateProcessWithLogonW(lpUsdeerName,lpDomain,lpPassword: LPCWSTR;
  dwLogonFlags:dword; lpApplicationName : LPCWSTR; lpCommandLine: LPWSTR; dwCreateionFlags:dword;
  lpEnvironment:Pointer; lpCurrentDirectory: LPCWSTR; const lpstartupinfo:tstartupinfo;
  var lpProcessInfo:TProcessInformation): BOOL; stdcall; external 'advapi32.dll';

function CreateProcessWithLogon(user, domain, password, cmdLine: string; var pi: TProcessInformation) : dword;
var si : TStartupInfo;
begin
  ZeroMemory(@si, sizeOf(si));
  si.cb := sizeOf(si);
  ZeroMemory(@pi, sizeOf(pi));
  result := CreateProcessWithLogonW(PWideChar(wideString(user)),
                                    PWideChar(wideString(domain)),
                                    PWideChar(wideString(password)),
                                    0, nil,
                                    PWideChar(wideString(cmdLine)),
                                    CREATE_DEFAULT_ERROR_MODE or NORMAL_PRIORITY_CLASS,
                                    nil, nil, si, pi);
  if not result then
       result := GetLastError
  else result := 0;
end;

Regards, Madshi.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question