最近项目中用到将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);
}
}