www.gusucode.com > 一些VC++加密算法+实例源码源码程序 > 一些VC++加密算法+实例源码/优化后的加密注册模块/优化后的加密注册模块/InvCall(optimized)/module.cpp
//**************************************************** //* Arko Information Technology Co.,Ltd. * //**************************************************** //*module.h Version: 1.0 * //*Copyright: This is a part of the Arko Source Code * //**************************************************** //*Programmmer: YL * //*Date: 2000-05-18 * //**************************************************** #include "stdafx.h" #include "module.h" LPWSTR MakeWideStrFromAnsi(LPSTR psz) { LPWSTR pwsz; int i; // arg checking. // if (!psz) return NULL; // compute the length of the required BSTR // i = MultiByteToWideChar(CP_ACP, 0, psz, -1, NULL, 0); if (i <= 0) return NULL; // allocate the widestr // -1 since it'll add it's own space for a NULL terminator // pwsz = (LPWSTR) SysAllocStringLen(NULL, i - 1); if (!pwsz) return NULL; MultiByteToWideChar(CP_ACP, 0, psz, -1, pwsz, i); pwsz[i - 1] = 0; return pwsz; } //**************************************************************************** // // LPTSTR AllocLPTSTR (ULONG cb) // // This function allocates a LPTSTR // //**************************************************************************** LPTSTR AllocLPTSTR (ULONG cb) { LPTSTR psz; psz = (LPTSTR)LocalAlloc(LMEM_FIXED, cb*sizeof(TCHAR)); return psz; } //**************************************************************************** // // HRESULT FreeLPTSTR (LPTSTR psz) // // This function frees the LPTSTR // //**************************************************************************** HRESULT FreeLPTSTR (LPTSTR psz) { HLOCAL hRet; hRet = LocalFree(psz); return( hRet == NULL ? S_OK : E_FAIL); } //**************************************************************************** // // HRESULT BSTR_to_LPTSTR (LPTSTR *ppsz, BSTR bstr) // //**************************************************************************** HRESULT BSTR_to_LPTSTR (LPTSTR *ppsz, BSTR bstr) { #ifndef UNICODE LPTSTR psz; int i; HRESULT hr; // Whoa... handle the NULL string first. // if(bstr == NULL) // if bstr is NULL we return E_UNEXPECTED and pass a default string { psz = AllocLPTSTR(lstrlen("")+1); if(psz != NULL) { strcpy(psz, ""); *ppsz = psz; } return E_UNEXPECTED; } // compute the length of the required BSTR // i = WideCharToMultiByte(CP_ACP, 0, (LPWSTR)bstr, -1, NULL, 0, NULL, NULL); if (i <= 0) { return E_UNEXPECTED; }; // allocate the widestr, +1 for terminating null // psz = AllocLPTSTR(i); if (psz != NULL) { WideCharToMultiByte(CP_ACP, 0, (LPWSTR)bstr, -1, psz, i, NULL, NULL); *ppsz = psz; hr = S_OK; } else { hr = E_OUTOFMEMORY; }; return hr; #else LPTSTR psz = NULL; HRESULT hr; hr = SetName(&psz, (LPTSTR)bstr); if (hr == S_OK) { *ppsz = psz; }; return hr; #endif // UNICODE } HRESULT __fastcall AnsiToUnicode(LPCSTR pszA, LPOLESTR* ppszW) { ULONG cCharacters; DWORD dwError; // If input is null then just return the same. if (NULL == pszA) { *ppszW = NULL; return NOERROR; } // Determine number of wide characters to be allocated for the // Unicode string. cCharacters = strlen(pszA)+1; // Use of the OLE allocator is required if the resultant Unicode // string will be passed to another COM component and if that // component will free it. Otherwise you can use your own allocator. *ppszW = (LPOLESTR) CoTaskMemAlloc(cCharacters*2); if (NULL == *ppszW) return E_OUTOFMEMORY; // Covert to Unicode. if (0 == MultiByteToWideChar(CP_ACP, 0, pszA, cCharacters, *ppszW, cCharacters)) { dwError = GetLastError(); CoTaskMemFree(*ppszW); *ppszW = NULL; return HRESULT_FROM_WIN32(dwError); } return NOERROR; } HRESULT __fastcall UnicodeToAnsi(LPCOLESTR pszW, LPSTR* ppszA) { ULONG cbAnsi, cCharacters; DWORD dwError; // If input is null then just return the same. if (pszW == NULL) { *ppszA = NULL; return NOERROR; } cCharacters = wcslen(pszW)+1; // Determine number of bytes to be allocated for ANSI string. An // ANSI string can have at most 2 bytes per character (for Double // Byte Character Strings.) cbAnsi = cCharacters*2; // Use of the OLE allocator is not required because the resultant // ANSI string will never be passed to another COM component. You // can use your own allocator. *ppszA = (LPSTR) CoTaskMemAlloc(cbAnsi); if (NULL == *ppszA) return E_OUTOFMEMORY; // Convert to ANSI. if (0 == WideCharToMultiByte(CP_ACP, 0, pszW, cCharacters, *ppszA, cbAnsi, NULL, NULL)) { dwError = GetLastError(); CoTaskMemFree(*ppszA); *ppszA = NULL; return HRESULT_FROM_WIN32(dwError); } return NOERROR; }