Re: [問題] 無法呼叫DLL程式
看板C_and_CPP (C/C++)作者littleshan (我要加入劍道社!)時間15年前 (2011/03/22 00:48)推噓4(4推 0噓 3→)留言7則, 3人參與討論串2/2 (看更多)
※ 引述《ncuoo (我是金順啦)》之銘言:
: 檔頭dll.h http://codepad.org/NYA2xYZ1
: #include <iostream>
: #include <string>
: #ifndef _DLL_H_
: #define _DLL_H_
1. 任何 identifier 都不要用底線+大寫開頭,那是留給 standard library 用的
2. include guard 應該寫在檔案最前面 (在 #include 之前)
: #if BUILDING_DLL
: # define DLLIMPORT __declspec (dllexport)
: #else /* Not BUILDING_DLL */
: # define DLLIMPORT __declspec (dllimport)
: #endif /* Not BUILDING_DLL */
: #ifdef __cplusplus
: using namespace std;
header file 不要用 using namespace
: extern "C" {
: DLLIMPORT void writefile_jacky(string write_order, string write_price);
: }
writefile_jacky 是使用 C convention 的函式
可是它的參數會接 C++ 物件
你不覺得哪裡怪怪的?
這樣做不是不行啦
但是既然你的 DLL 是要拿來 dynamic load
那傳遞 C++ 物件之前可能要確保一下 client 和你使用相同版本的 compiler
: #endif /* _DLL_H_ */
少了一個 #endif
你忘了 __cplusplus 嗎?
: dll主程式:dllmain.cpp http://codepad.org/qT2XWrhU
: #include <iostream>
: #include <fstream>
: #include <string>
: #include <stdio.h>
: #include <stdlib.h>
少了 #include "dll.h"
沒有它的話 compiler 就不知道你的 writefile_jacky 是要拿來 export 的
所以 LoadLibrary 之後會找不到那個 symbol
(本來覺得很奇怪 你少了一個 #endif 應該會有 compile error 才對)
(結果你是根本沒 include!)
[以下略]
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.168.83.71
推
03/22 01:01, , 1F
03/22 01:01, 1F
→
03/22 10:50, , 2F
03/22 10:50, 2F
推
03/22 12:24, , 3F
03/22 12:24, 3F
→
03/22 12:26, , 4F
03/22 12:26, 4F
推
03/23 20:09, , 5F
03/23 20:09, 5F
推
03/23 20:38, , 6F
03/23 20:38, 6F
→
03/24 10:57, , 7F
03/24 10:57, 7F
討論串 (同標題文章)
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章