查找当前程序目录中以32和64结束的exe程序的名称

最近项目中用到将32位和64位的exe程序打包成一个安装包,根据执行的系统平台执行不同的exe程序。

所以在这个两个程序中需要加一个程序来判断平台并调用32或者64位的exe程序。 

代码如下:

// Exec32Or64Program.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>
#include <string>

void FindexeAndRun(int param);

typedef enum PLATFORM
{
    WINX86=1,
    WINX64=2,
};
const int FILE_PATH = 255;

int _tmain(int argc, _TCHAR* argv[])
{
    SYSTEM_INFO  si;
       ::GetNativeSystemInfo(&si);
    if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ||   
        si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64 )  
    {  
        FindexeAndRun(WINX64);
    }
    else
    {
        FindexeAndRun(WINX86);
    }
    return 0;
}


//param:输入1表示执行当前目录下的32位程序
//            :输入2表示执行当前目录下的64位程序
void FindexeAndRun(int param)
{
    //获取当前目录的文件
    wchar_t exeabname[FILE_PATH]={L"0"};
    wchar_t pstrPath[FILE_PATH] = {L"0"};

    ::GetModuleFileName(NULL,exeabname,FILE_PATH);
    LPCWSTR pstrSep=wcsrchr(exeabname,L'\\');
    if(pstrSep!=NULL)
    {
        wchar_t pstrName[FILE_PATH] = {L"0"};
        ::lstrcpy(pstrName,pstrSep+1);
        int result=(int)((pstrName-exeabname)/sizeof(wchar_t));
        ::lstrcpyn(pstrPath,exeabname,(int)((pstrName-exeabname)/sizeof(wchar_t)+1));
        pstrPath[pstrSep-exeabname]='\0';
    }

    wchar_t progX86[FILE_PATH] = {L"0"};
    wchar_t progX64[FILE_PATH] = {L"0"};

    wchar_t FindDirectoryPath[FILE_PATH]={L"0"};
    wsprintf(FindDirectoryPath,L"%s\\*.*",pstrPath);
    WIN32_FIND_DATA wfd;
    HANDLE hFind;
    if((hFind=FindFirstFile(FindDirectoryPath,&wfd))==INVALID_HANDLE_VALUE)
        return ;
    do
    {
        if((wcscmp(wfd.cFileName,L".")==0||wcscmp(wfd.cFileName,L"..")==0||
            wcscmp(wfd.cFileName,L"desktop.ini")==0))
            {
                continue;
           }
        
        int len = wcslen(wfd.cFileName);
        int exelen = wcslen(L".exe");
        if((wfd.cFileName[len-exelen-2] == L'3') && (wfd.cFileName[len-exelen-1] == L'2'))
        {
            wsprintf(progX86,L"%s\\%s",pstrPath,wfd.cFileName);
        }
        if((wfd.cFileName[len-exelen-2] == L'6') && (wfd.cFileName[len-exelen-1] == L'4'))
        {
            wsprintf(progX64,L"%s\\%s",pstrPath,wfd.cFileName);
        }
    }
    while(FindNextFile(hFind,&wfd));
        FindClose(hFind);

    if( WINX86 == param )
    {
        MessageBox(NULL,progX86,L"Msg",0);
       ShellExecute(NULL,L"open", progX86, L"", L"", SW_HIDE);
    }
    else if( WINX64 == param )
    {
      MessageBox(NULL,progX64,L"Msg",0);
      ShellExecute(NULL,L"open", progX64, L"", L"", SW_HIDE);
    }
    else
    {
        MessageBox(NULL,L"err",L"Msg",0);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值