Win32 API的运行.exe文件等 [英] win32 API for running other .exe

查看:136
本文介绍了Win32 API的运行.exe文件等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在Win32 API运行 REGSVR32 编程。

I wanted to know the win32 API to run the regsvr32 programatically.

我有一个应用程序,通过它我可以用COM口设备进行通信,但要实现通信,我必须注册 MSCOMM32.OCX 使用 REGSVR32 .EXE

I have one application through which I can communicate with COM port devices, but to achieve communication, I have to register MSCOMM32.ocx using regsvr32.exe.

但现在我想补充这一规定在我的code,所以没有必要跑 regsvr 手动

But now I wanted to add this provision in my code, so that there is no need to run regsvr manually.

推荐答案

MSDN是你的朋友。

MSDN is your friend.

  • How Regsvr32.exe Registers and Unregisters COM DLLs
  • COM Server Responsibilities

我要复制并粘贴code的:

I'll copy and paste the code in:

    #include <tchar.h>
    #include <afxole.h>
    #include <stdlib.h>

    #define FAIL_ARGS    1
    #define FAIL_OLE     2
    #define FAIL_LOAD    3
    #define FAIL_ENTRY   4
    #define FAIL_REG     5 

    static char szAppName[] = "Register";
    static char szUsage[] = "\n\nUsage: Register [/u] dllname";
    static char szDllRegSvr[] = "DllRegisterServer";
    static char szDllUnregSvr[] = "DllUnregisterServer"; 

    int PASCAL WinMain(
               HINSTANCE hInstance, 
               HINSTANCE hPrev, 
               LPSTR pszCmdLine, 
               int nCmdShow)
    {   
        int iReturn = 0;       
        HRESULT (FAR STDAPICALLTYPE * lpDllEntryPoint)(void);  
        static TCHAR szMsgBuffer[_MAX_PATH*4];         
        BOOL bVisualC = FALSE; 
        BOOL bSilent = FALSE;  
        BOOL bUnregister = FALSE;      
        LPSTR pszDllEntryPoint = szDllRegSvr;  
        LPSTR pszDllName = NULL;
        char szCmdLineCopy[_MAX_PATH]; 
        strcpy(szCmdLineCopy, pszCmdLine);             
        LPSTR pszTmp = szCmdLineCopy;  
        LPSTR pszTok;          

        while ((pszTok = strtok(pszTmp, " \t")) != NULL)       
        {                                
            pszTmp = NULL; 

            if ((pszTok[0] == '-') || (pszTok[0] == '/'))
            {      
                switch (pszTok[1])     
                {      
                case 'v':      
                case 'V':
                    bVisualC = TRUE;
                    break;         
                case 's':      
                case 'S':
                    bSilent = TRUE;
                    break;         
                case 'u':      
                case 'U':
                    bUnregister = TRUE;
                    pszDllEntryPoint = szDllUnregSvr;
                    break;         
                default:
                    wsprintf(szMsgBuffer, 
                        "Unrecognized flag: %s%s", 
                        pszTok, 
                        (LPCSTR)szUsage);
                    if (!bSilent)  
                        MessageBox(NULL, 
                           szMsgBuffer, 
                           szAppName, 
                           MB_TASKMODAL | MB_ICONEXCLAMATION);
                    return FAIL_ARGS;      
                }
            }
            else
            {      
                if (pszDllName == NULL)
                    pszDllName = pszTok;   
                else   
                {
                    wsprintf(szMsgBuffer, 
                        "Extra argument on command line: %s%s", 
                        pszTok, 
                        (LPCSTR)szUsage);
                    if (!bSilent)  
                        MessageBox(NULL, 
                           szMsgBuffer, 
                           szAppName, 
                           MB_TASKMODAL | MB_ICONEXCLAMATION);
                    return FAIL_ARGS;      
                }
            }      
        }      

        if (pszDllName == NULL)        
        {
            if (!bSilent)
            {      
                if (bVisualC)  
                {
                    MessageBox(NULL, 
                    "This command is only valid when "
                    "an OLE Custom Control project is open.", 
                    bUnregister ? 
                    "Unregister Control" : "Register Control",
                    MB_TASKMODAL | MB_ICONEXCLAMATION);


    }
            else   
            {
                wsprintf(szMsgBuffer, 
                    _T("No DLL name specified%s"), 
                    (LPCSTR)szUsage);
                MessageBox(NULL, 
                    szMsgBuffer, 
                    szAppName, 
                    MB_TASKMODAL | MB_ICONEXCLAMATION);
            }
        }
        return FAIL_ARGS;      
    }

    if (FAILED(OleInitialize(NULL)))       
    {
        if (!bSilent)  
            MessageBox(NULL, 
                "OleInitialize failed.", 
                szAppName, 
                MB_TASKMODAL | MB_ICONINFORMATION);
        return FAIL_OLE;       
    }              

    HINSTANCE hLib = LoadLibrary(pszDllName);      

    if (hLib < (HINSTANCE)HINSTANCE_ERROR) 
    {
        wsprintf(szMsgBuffer, 
            "LoadLibary(\"%s\") failed.", 
            pszDllName);
        MessageBox(NULL, 
            szMsgBuffer, 
            szAppName, 
            MB_TASKMODAL | MB_ICONEXCLAMATION);
        iReturn = FAIL_LOAD;
        goto CleanupOle;       
    }

    (FARPROC&)lpDllEntryPoint = GetProcAddress(hLib, pszDllEntryPoint);

    if (lpDllEntryPoint == NULL)   
    {
#ifdef _WIN32
        int nLen = strlen(pszDllName);
        if ((nLen > 4) && 
            (stricmp(pszDllName + nLen - 4, ".dll") != 0)  && 
            (stricmp(pszDllName + nLen - 4, ".ocx") != 0))
        {      
            wsprintf(szMsgBuffer, 
            "%s was loaded, but the %s entry point "
            "was not found. %s does not appear to be "
            "an .DLL or .OCX file.", 
            pszDllName, 
            pszDllEntryPoint, 
            pszDllName);
        }
        else
        {      
            wsprintf(szMsgBuffer, 
            "%s was loaded, but the %s entry point "
            "was not found. %s may not be exported, "
            "or a corrupt version may be in memory.  "
            "Consider using PView to detect and remove it.",
            pszDllName, 
            pszDllEntryPoint, 
            pszDllEntryPoint);
        }
#else
        wsprintf(szMsgBuffer, 
        "%s was loaded, but the %s entry point "
        "was not found. %s may not be exported, "
        "or a corrupt version may be in memory.  "
        "Consider using WPS to detect and remove it.",
        pszDllName, 
        pszDllEntryPoint, 
        pszDllEntryPoint);
#endif

        if (!bSilent)  
            MessageBox(NULL, 
                szMsgBuffer, 
                szAppName, 
                MB_TASKMODAL | MB_ICONEXCLAMATION);
        iReturn = FAIL_ENTRY;

        goto CleanupLibrary;   
    }

    if (FAILED((*lpDllEntryPoint)()))      
    {
        wsprintf(szMsgBuffer, 
            "%s in %s failed.", 
            pszDllEntryPoint, 
            pszDllName);

        if (!bSilent)  
            MessageBox(NULL, 
                szMsgBuffer, 
                szAppName, 
                MB_TASKMODAL | MB_ICONEXCLAMATION);
        iReturn = FAIL_REG;

        goto CleanupLibrary;   
    }      

    wsprintf(szMsgBuffer,
        "%s in %s succeeded.", 
        pszDllEntryPoint, 
        pszDllName);     

    if (! bSilent)
        MessageBox(NULL, 
            szMsgBuffer, 
            szAppName, 
            MB_TASKMODAL | MB_ICONINFORMATION); 
CleanupLibrary:     
    FreeLibrary(hLib); 

CleanupOle: 
    OleUninitialize();     

    return iReturn;
}

这篇关于Win32 API的运行.exe文件等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆