作者commandoEX (卡曼都)
看板C_and_CPP
标题[问题]DLL在VC2017 Debug模式下抓不到指定档案
时间Wed Sep 12 01:03:28 2018
开发平台(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://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1536685410.A.539.html
※ 编辑: commandoEX (140.115.220.131), 09/12/2018 01:04:41
1F:推 eye5002003: #elif defined(_WIN32) 就是else if啦,很多程式都用 09/12 10:48
2F:→ eye5002003: _WIN32来判断平台,所以x64一样抓的到,先抓_WIN64就解 09/12 10:50
3F:→ eye5002003: 决了。system("pause")就是用来卡住的。debug模式抓的 09/12 10:52
4F:→ eye5002003: 资料夹位置好像是专案根目录...吧 09/12 10:54
感谢大大的提点
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