我有一個MFC和一個普通的dll,不是mfc dll,在使用mfc呼叫dll中函式的時候有的函式可以呼叫成功,有的不成功。
MFC中的呼叫陳述句:
strSQL.Format("update wageinfo set wage = %lf, date = '%s' where id = '%s'", m_nWage, strDate, m_strID);
int rvsql = dboperafunc(strSQL);
if (rvsql != 0)
DBOPERA_ERROR;
定義的dll中的dboperafunc函式:
DBOPERADLL_API int dboperafunc(const CString& sql)
{
try {
_ConnectionPtr pConnection;
HRESULT hr = pConnection.CreateInstance("ADODB.Connection"); //創建Connection物件
if (SUCCEEDED(hr))
hr = pConnection->Open("driver={SQL Server};Server=(local);DATABASE=HMSys;", "", "", adModeUnknown);
//_ConnectionPtr m_pConnection = openDb();
pConnection->Execute((_bstr_t)sql, NULL, adCmdText);
return 0;
}
catch (_com_error e)
{
CString strComError;
strComError.Format("錯誤編號: %08lx\n錯誤資訊: %s\n錯誤源: %s\n錯誤描述: %s",
e.Error(), // 錯誤編號
e.ErrorMessage(), // 錯誤資訊
(LPCSTR)e.Source(), // 錯誤源
(LPCSTR)e.Description()); // 錯誤描述
::MessageBox(NULL, strComError, "錯誤", MB_ICONEXCLAMATION);
return e.Error();
}
}
dll是可以編譯生成的,有.lib和.dll檔案。MFC的屬性中也已經正確的鏈接了dll生成的.lib檔案,但在編譯MFC的時候會提示下面的錯誤:
錯誤 LNK2019 無法決議的外部符號 "__declspec(dllimport) int __cdecl dboperafunc(class ATL::CStringT<char,class StrTraitMFC_DLL<char,class ATL::ChTraitsCRT<char> > > const &)" (__imp_?dboperafunc@@YAHABV?$CStringT@DV?$StrTraitMFC_DLL@DV?$ChTraitsCRT@D@ATL@@@@@ATL@@@Z),該符號在函式 __catch$?OnSave@CWageManageDlg@@IAEXXZ$0 中被參考
有沒有好心人提示一下哪里出錯了???謝謝
uj5u.com熱心網友回復:
1. 在C/C++目錄中包含dll對應的頭檔案,庫目錄包含dll檔案2.在聯結器-》輸入選項中添加dll檔案對應的lib檔案
3.生成DLL檔案的字符集和呼叫dll的exe的字符集是否統一?
uj5u.com熱心網友回復:
CoInitialize(NULL);
//……
CoUninitialize();
?
uj5u.com熱心網友回復:
統一的,都是多位元組
uj5u.com熱心網友回復:
這個是在哪里添加?能不能細說一下?謝謝
uj5u.com熱心網友回復:
感覺也不是這個問題,試了一下還是一樣的錯
uj5u.com熱心網友回復:
https://bbs.csdn.net/topics/200039885uj5u.com熱心網友回復:
https://www.baidu.com/s?wd=MFC%20ado%20CoInitialize%20CoInitializeExuj5u.com熱心網友回復:
最后參考的這個https://bbs.csdn.net/topics/398268320
好像普通的dll中使用MFC中的CString會有問題,最后改成了char *可以了。
CString轉char *參考的這個:
https://blog.csdn.net/maodewen11/article/details/107070267/
感謝各位大佬了
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/242588.html
標籤:C++ 語言
上一篇:求助,請各位大神幫幫忙
