Friday, September 22, 2006

Smart Pointer 是什麼?

最近為了寫資料庫的程式,由網路上得知可以使用import ADO 元件來輕鬆達成目的。的確程式的開發相當容易,不過據說是因為使用 Smart Pointer 技術所達到的,但是卻不清楚 Smart Pointer 到底是什麼東西。根據 News 上 Jeff Henkels 的回答:

Because the smart pointer is allocated on the stack, it will be destroyed
when it goes out of scope -- its destructor will release the recordset
object.


For more details, look at the smart pointer template class _com_ptr_t,
implemented in comip.h in the MSVC++ INCLUDE directory

真是佩服這些程式開發人員。

Thursday, September 14, 2006

ATL 元件 Compile Error

今天在建立一個 ATL 元件時,Compile release 版本時發生
_free already defined in StdAfx.obj 的錯誤訊息
在論壇上看見一位熟客 Alexander Nickolov 他解答了我的問題

Remove _ATL_MIN_CRT from your build settings. The C RTL
is required by MFC.

竟是這麼簡單

VC++與VB的型態對應

VC++ VB


short Integer

int Long

long Long

UNIT Long

ULONG Long

WORD,DWORD Long

WPARAM,LPARAM Long

WMSG,UMSG Long

HRESULT Long

BOOL Boolean

COLORREF Long

HWND,HDC,HBRUSH,HKEY Long

LPSTR,LPCSTR String

LPWSTR,OLECHAR,BSTR String

LPTSTR String

VARIANT_BOOL Boolean

unsignedchar Byte

BYTE Byte

VARIANT Variant

Monday, September 11, 2006

要如何讓 DLL 讀取其所在目錄的 INI 檔

之前在遇到此問題時,是以 GetCurrentDirectory 來處理,結果會有問題。因為執行時在 DLL 中呼叫此 API 會得到 EXE 檔的目錄,而非 DLL 檔的目錄。以下的程式碼可以解決這個問題:

// With STL string
#include

char szAppPath[MAX_PATH] = "";
std::string strAppDirectory;

::GetModuleFileName(0, szAppPath, sizeof(szAppPath) - 1);

// Extract directory
strAppDirectory = szAppPath;
strAppDirectory = strAppDirectory.substr(0, strAppDirectory.rfind("\\"));



// With CString
char szAppPath[MAX_PATH] = "";
CString strAppDirectory;

::GetModuleFileName(0, szAppPath, sizeof(szAppPath) - 1);

// Extract directory
strAppDirectory = szAppPath;
strAppDirectory = strAppDirectory.Left(strAppDirectory.ReverseFind('\\'));



// With standard string
char szAppPath[MAX_PATH] = "";
char szAppDirectory[MAX_PATH] = "";

::GetModuleFileName(0, szAppPath, sizeof(szAppPath) - 1);

// Extract directory
strncpy(szAppDirectory, szAppPath, strrchr(szAppPath, '\\') - szAppPath);
szAppDirectory[strlen(szAppDirectory)] = '\0';

Friday, September 08, 2006

Unicode 與 UTF-8 的互轉

Unicode 與 UTF-8 的互轉可以使用下面的方式

Unicode 轉 UTF-8:
char buff[1024];
BSTR sInData;
pwBuffer = (wchar_t*)sInData;
iCharLen = lstrlenW(sInData);
iByteLen = WideCharToMultiByte(CP_UTF8, 0, pwBuffer, iCharLen, buff, 1023, 0, 0);
buff[iByteLen] = 0;

UTF-8 轉 Unicode:
BSTR sOutData;
buff[r] = 0;
iCharLen = MultiByteToWideChar(CP_UTF8, 0, buff, r, NULL, 0);
sOutData = SysAllocStringLen(0, iCharLen);
iCharLen = MultiByteToWideChar(CP_UTF8, 0, buff, r, sOutData, iCharLen);

Friday, September 01, 2006

使用 AllocSysString

使用 AllocSysString 會產生 error LNK2001: unresolved external symbol "public: unsigned short * __thiscall CString::AllocSysString(void)const " (?AllocSysString@CString@@QBEPAGXZ)的錯誤,透過偉大 Google 的查詢,找到了解答,原來是要在 Link Modules 中加入 mfco42d.lib。