Last active
October 5, 2021 09:30
-
-
Save earlgreyxxx/e7a529a835ec7931aed9a31e01e38060 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define GLOBALALLOC(S) reinterpret_cast(GlobalAlloc(GPTR,(S))) | |
#define GLOBALFREE(X) {if(X){GlobalFree(reinterpret_cast(X));(X)=NULL;}} | |
#define HEAPALLOC(S) HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(S)) | |
#define HEAPFREE(X) {if(X){HeapFree(GetProcessHeap(),0,reinterpret_cast(X));(X)=NULL;}} | |
PSTR *CommandLineToArgvA(LPCSTR lpCmdLine,int *pNumArgs) | |
{ | |
PSTR *pArgvA = NULL; | |
int Argc = 0; | |
PWSTR *pArgvW = NULL; | |
PWSTR pM2WBuffer = NULL; | |
int *pcchLen = NULL; | |
int cchBufferLength = 0; | |
*pNumArgs = 0; | |
if((cchBufferLength = MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,lpCmdLine,-1,NULL,0)) | |
goto cleanup; | |
if((pM2WBuffer = (PWSTR)HEAPALLOC((cchBufferLength+1)*sizeof(WCHAR))) == NULL) | |
goto cleanup; | |
MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,lpCmdLine,-1,pM2WBuffer,cchBufferLength+1); | |
pArgvW = CommandLineToArgvW(pM2WBuffer,&Argc); | |
//コマンドラインをマルチバイトに変換した後の | |
//文字数(配列pcchLen)、文字数合計(cchBufferLength)を求める | |
if((pcchLen = (int*)HEAPALLOC(sizeof(int)*Argc)) == NULL) | |
goto cleanup; | |
cchBufferLength = 0; | |
for(int i=0;i<Argc;i++) { | |
pcchLen[i] = WideCharToMultiByte(CP_ACP,0,pArgvW[i],-1,NULL,0,NULL,NULL); | |
cchBufferLength += ++pcchLen[i]; | |
} | |
//コマンドライン文字列の配列を取得して格納する。 | |
if((pArgvA = (PSTR*)GLOBALALLOC(cchBufferLength + sizeof(PSTR)*Argc)) == NULL) | |
goto cleanup; | |
for(int i=0;i<Argc;i++) { | |
pArgvA[i] = (i < 1) ? (PSTR)pArgvA + sizeof(PSTR)*Argc : pArgvA[i-1] + pcchLen[i-1]; | |
WideCharToMultiByte(CP_ACP,0,pArgvW[i],-1,pArgvA[i],pcchLen[i],NULL,NULL); | |
} | |
cleanup: | |
HEAPFREE(pcchLen); | |
HEAPFREE(pM2WBuffer); | |
GLOBALFREE(pArgvW); // 解放処理を忘れていたので追加しました。(2010/7/4) | |
*pNumArgs = Argc; | |
return pArgvA; | |
} |
oh, sorry! i failed to copy & paste from my file to gist's text editor.
i fixed them.
thanks for your comment
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, can you fix your code so i can read and learning this code? I confuse in part "for(int i=0;i {" also can you write code that easly to be read? like if(condition){//block to be executed}. please use "{" or "}". thank you