【问题标题】:CreateProcessAsUser, call dll functionCreateProcessAsUser,调用dll函数
【发布时间】:2012-06-25 21:20:05
【问题描述】:

使用 CreateProcessAsUser,我可以在硬盘的某处调用 .exe 文件:

CreateProcessAsUser(IntPtr hToken, string lpApplicationName, string lpCommandLine,
                      ref SECURITY_ATTRIBUTES lpProcessAttributes, ref SECURITY_ATTRIBUTES lpThreadAttributes,
                      bool bInheritHandle, Int32 dwCreationFlags, IntPtr lpEnvrionment,
                      string lpCurrentDirectory, ref STARTUPINFO lpStartupInfo,
                      ref PROCESS_INFORMATION lpProcessInformation);

我在网上找到的每个示例都使用 lpCommandLine 参数来调用程序。 我想在dll中调用一个函数。 有谁知道这是否可能?要是能举个例子就好了……

谢谢!

【问题讨论】:

  • @RemusRusanu 我需要使用 CreateProcessAsUser 因为我需要从服务中调用它,而我必须调用的程序或方法需要一些用户权限。

标签: c# winapi createprocessasuser


【解决方案1】:

您不能以不同用户的身份直接调用 DLL,因为用户/执行级别是每个进程,而不是 DLL 或线程。您必须启动一个新进程,然后调用该 DLL。这是使用的技术 COM 提升等。如果 DLL 具有正确的签名,您可以尝试使用 rundll32.exe 调用它。

【讨论】:

    【解决方案2】:

    我认为使用该功能是不可能的。在 dll 中调用方法的标准方法是使用 LoadLibraryGetProcAddress 方法,如下例所示:

    (取自MSDN

    // A simple program that uses LoadLibrary and 
    // GetProcAddress to access myPuts from Myputs.dll. 
    
    #include <windows.h> 
    #include <stdio.h> 
    
    typedef int (__cdecl *MYPROC)(LPWSTR); 
    
    int main( void ) 
    { 
        HINSTANCE hinstLib; 
        MYPROC ProcAdd; 
        BOOL fFreeResult, fRunTimeLinkSuccess = FALSE; 
    
        // Get a handle to the DLL module.
    
        hinstLib = LoadLibrary(TEXT("MyPuts.dll")); 
    
        // If the handle is valid, try to get the function address.
    
        if (hinstLib != NULL) 
        { 
            ProcAdd = (MYPROC) GetProcAddress(hinstLib, "myPuts"); 
    
            // If the function address is valid, call the function.
    
            if (NULL != ProcAdd) 
            {
                fRunTimeLinkSuccess = TRUE;
                (ProcAdd) (L"Message sent to the DLL function\n"); 
            }
            // Free the DLL module.
    
            fFreeResult = FreeLibrary(hinstLib); 
        } 
    
        // If unable to call the DLL function, use an alternative.
        if (! fRunTimeLinkSuccess) 
            printf("Message printed from executable\n"); 
    
        return 0;
    
    }
    

    【讨论】:

    • 不幸的是,我需要使用 CreateProcessAsUser,因为我需要从服务中调用它,而我必须调用的程序或方法需要一些用户权限。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多