[問題]DLL在VC2017 Debug模式下抓不到指定檔案

看板C_and_CPP作者 (卡曼都)時間7年前 (2018/09/12 01:03), 7年前編輯推噓1(103)
留言4則, 1人參與, 7年前最新討論串1/1
開發平台(Platform): (Ex: Win10, Linux, ...) windows 10 1803 編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出) Visual Studio 2017 社群版(15.8.3) 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 小弟我主要是寫delphi的,幾乎沒碰過c++(更之前只有稍微碰過C/VB) 今天BOSS想要把實驗室中某個程式給其他老師用 因為外面根本沒啥人用delphi所以叫我改寫成dll給其他實驗室的人呼叫 dll本身只有一個程式,沒有input參數也沒output參數 只讀預定義的設定檔和寫固定的結果檔 為了方便對方使用,我用delphi、C++、C#都寫了一個程式碼檔載入dll定義好函式 讓對方可以直接呼叫程式不用再自己引入dll delphi和C#都測試能正常運行,就C++問題解決不了 主要有三個問題: 1.如標題所說的VS使用debug模式下程式抓不到設定檔,不過如果直接進資料夾點程式 是抓的到的 2.dll的程式執行完後測試用的主控台程式似乎就卡住了,一直沒有執行到最後一行 system("pause"); 3.因為dll我編譯了x86和x64版,所以在C++中用#ifdef去抓程式要載入哪個dll 但是WIN32不管是在x86還是x64下都是true,目前是編譯環境在x64時用#undef WIN32 讓系統只抓到 _WIN64的條件,不知道是否有更好的解法? 餵入的資料(Input):預期的正確結果(Expected Output): dll回傳訊息"計算完成" 錯誤結果(Wrong Output): dll回傳訊息"未檢查到Data.csv" 程式碼(Code):(請善用置底文網頁, 記得排版,禁止使用圖檔) -----LoadDLL.h---- #pragma once #ifdef _WIN64 #undef WIN32 #endif extern void Run(); -----LoadDLL.cpp---- #include <stdio.h> #include <Windows.h> #include "LoadDLL.h" typedef void(*importFunction)(); void Run() { importFunction TempFunc; #ifdef _WIN64 HINSTANCE hinstLib = LoadLibrary(TEXT("DelphiDLL_x64.dll")); #endif // WIN64 #ifdef WIN32 HINSTANCE hinstLib = LoadLibrary(TEXT("DelphiDLL_x86.dll")); #endif // WIN32 if (hinstLib == NULL) { printf("ERROR: unable to load DLL\n"); system("pause"); } TempFunc = (importFunction)GetProcAddress(hinstLib, "Run"); if (TempFunc == NULL) { printf("ERROR: unable to find DLL function\n"); FreeLibrary(hinstLib); system("pause"); } else { TempFunc(); FreeLibrary(hinstLib); system("pause");//執行檔沒運行到這 } } -----main.cpp----- #include "stdafx" #include "LoadDLL.h" int main(){ Run(); system("pause"); return 0; } -------DelphiDLL--------- (此處是delphi程式語言,並且省略實際運行內容) Libary DelphiDLL; uses System.SysUtils, System.Classes, windows, vcl.dialogs, math; {$R *.res} procedure Run;stdcall; begin if FileExists('Data.csv') then begin DoSomething; ShowMessage('計算完成'); end else ShowMessage('未檢查到Data.csv'); end; exports Run; end. 補充說明(Supplement): 原本在main中載入dll測試時可以正常抓到Data.csv 也會運行到最後的system("pause"); 在此先感謝各位耐心觀看 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.115.220.131 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1536685410.A.539.html ※ 編輯: commandoEX (140.115.220.131), 09/12/2018 01:04:41

09/12 10:48, 7年前 , 1F
#elif defined(_WIN32) 就是else if啦,很多程式都用
09/12 10:48, 1F

09/12 10:50, 7年前 , 2F
_WIN32來判斷平台,所以x64一樣抓的到,先抓_WIN64就解
09/12 10:50, 2F

09/12 10:52, 7年前 , 3F
決了。system("pause")就是用來卡住的。debug模式抓的
09/12 10:52, 3F

09/12 10:54, 7年前 , 4F
資料夾位置好像是專案根目錄...吧
09/12 10:54, 4F
感謝大大的提點 Data.csv放在專案根目錄就有抓到了沒錯 而加入#elif 的確能處理x64平台下同時符合_WIN64 _WIN32的問題 不過在我的認知中如果有執行到system("pause")應該會跳出一行"請按任意鍵繼續..." 但在執行完dll後這行一直沒跑出來才覺得奇怪 剛剛看了一下程式的log,最後停在 Thread Exiting: 8072 Thread Exiting: 16192 Thread Exiting: 13796 Thread Exiting: 22404 Thread Exiting: 9468 Thread Exiting: 19180 Thread Exiting: 9612 Thread Exiting: 23052 Thread Exiting: 16676 Thread Exiting: 4348 Thread Exiting: 11856 Thread Exiting: 20844 Thread Exiting: 17880 Thread Exiting: 21128 Thread Exiting: 1340 Thread Exiting: 14744 0x4748 執行緒以返回碼 0 (0x0) 結束。 0x1518 執行緒以返回碼 0 (0x0) 結束。 0x1178 執行緒以返回碼 0 (0x0) 結束。 這個dll裡面有用到delphi的平行運算函式庫(Parallel Programming Library) 會是這個原因嗎? ※ 編輯: commandoEX (140.115.66.73), 09/12/2018 17:00:10
文章代碼(AID): #1Rb_LYKv (C_and_CPP)