在程序中有时候需要用到连接字符串,但是有可能在配置时要改变它,手工输入是容易错的,有了这个对话框,一切就简单了,但是这个对话框可不是一个api就能调出来的,偶查了查msdn再东抄抄西抄抄再自己发了一会儿呆些了下面的这个函数。里面的东西不用你看懂,把它copy下来用就行乐,当然能看懂最好了,找出错误来请发emailto:[email protected].
BOOL GetConnectString(TCHAR *lpszConnectString,HWND hWndParent)
{
CComPtr<IDBPromptInitialize> pIDBPromptInitialize=NULL;
CComPtr<IDBProperties > pIDBProperties = NULL;
CComPtr<IDataInitialize> spDataInit=NULL;
CComBSTR bstrConnectString;
HRESULT hr;
// Create an instance of the Data Links component.
hr=CoCreateInstance(CLSID_DataLinks, NULL, CLSCTX_INPROC_SERVER,
IID_IDBPromptInitialize, (void **)&pIDBPromptInitialize);
if (FAILED(hr))
return FALSE;
// Prompt the user for the provider and initialization properties.
hr=pIDBPromptInitialize->PromptDataSource(NULL, hWndParent,
DBPROMPTOPTIONS_PROPERTYSHEET, 0, NULL, NULL, IID_IDBProperties,
(IUnknown **)&pIDBProperties);
if (FAILED(hr))
return FALSE;
hr = CoCreateInstance(CLSID_MSDAINITIALIZE, NULL, CLSCTX_INPROC_SERVER,
IID_IDataInitialize, (void**)&spDataInit);
if (FAILED(hr))
return FALSE;
hr=spDataInit->GetInitializationString(pIDBProperties,true,&bstrConnectString);
if (FAILED(hr))
return FALSE;
lstrcpy(lpszConnectString,(char*)_bstr_t(bstrConnectString));
return TRUE;
}
参数说明,返回值为false,用户取消
第二个为out,输入连接字符串
第三个位in,父窗口
记住先要初始化com哦
本文地址:http://com.8s8s.com/it/it2820.htm