- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经用 Delphi 编写了一个 DLL,现在我需要从 C++ 代码测试这个 DLL 的一个功能。
我以前从未使用过 C++。我安装了 Code::Blocks 并尝试用 C++ 编写代码。
返回整数或 bool 值的函数没有问题,但返回 BSTR
类型的函数有问题:
error: cannot convert 'GetLastErrText_funtype {aka wchar_t* (__attribute__((__stdcall__)) *)()}' to 'BSTR {aka wchar_t*}' in initialization
Delphi function header:
function GetLastErrText: BSTR; stdcall;
C++代码:
#include <iostream>
#include <cstdlib>
#include <windows.h>
using namespace std;
int main()
{
HINSTANCE dll_module = LoadLibrary("test.dll");
if(dll_module == NULL) { return -1; }
FARPROC GetLastErrText = GetProcAddress(dll_module, "GetLastErrText");
typedef BSTR (__stdcall* GetLastErrText_funtype)();
std::cout << "\nGetLastErrText = " << (void*)GetLastErrText;
if (GetLastErrText != 0) {
BSTR bstr = (GetLastErrText_funtype) GetLastErrText(); // <<<< ERROR
std::cout << "\nstr = " << bstr;
}
FreeLibrary(dll_module);
std::cout << "\n";
return 0;
}
如何修复此代码?
更新(阅读 Remy Lebeau 的评论后):
Delphi 中非常简化的代码
library test_ws;
uses
System.SysUtils,
Vcl.Dialogs;
function GetLastErrText: WideString; stdcall;
begin
try
Result := 'This is the result of GetLastErrText';
except
on E:Exception do
ShowMessage('Delphi exception::GetLastErrText : ' + E.Message); // when call from C++ code : Out of memory
end;
end;
exports
GetLastErrText;
begin
end.
根据 C++ 中的代码:
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
HMODULE lib = LoadLibrary("test_ws.dll");
typedef BSTR (__stdcall *Func)();
Func GetLastErrText = (Func) GetProcAddress(lib, "GetLastErrText");
BSTR bstr = GetLastErrText(); // <<<--- Out of memory
std::wcout << bstr;
SysFreeString(bstr);
return 0;
}
最佳答案
您尝试先调用 DLL 函数,然后将其返回值类型转换为函数指针。您不能将函数指针分配给 BSTR
指针,这是编译器所提示的。
您需要先对函数指针进行类型转换,然后才能正确调用该函数。您正在尝试在函数调用时执行此操作,但要正确执行此操作,您需要改为这样做:
BSTR bstr = ((GetLastErrText_funtype)GetLastErrText)(); // <-- note the extra parenthesis around the cast!
否则,最好在第一次获取函数指针时强制转换它,然后在需要时可以像普通函数一样调用它:
typedef BSTR (__stdcall* GetLastErrText_funtype)();
GetLastErrText_funtype GetLastErrText = (GetLastErrText_funtype) GetProcAddress(dll_module, "GetLastErrText");
...
BSTR bstr = GetLastErrText();
不要忘记在使用完 BSTR
后释放它(前提是 DLL 使用 SysAllocString...()
函数之一正确分配它):
BSTR bstr = GetLastErrText();
std::cout << "\nstr = "; // <-- std::cout does not support wchar_t data!
std::wcout << bstr; // <-- use std::wcout instead...
SysFreeString(bstr); // <-- free it!
更新:您的 DLL 的 Delphi 代码实际上并未返回原始 BSTR
指针,就像您的 C++ 代码所期望的那样。它实际上返回一个 WideString
,它是 Delphi 中的一个托管类型,并且总是通过一个隐藏的输出参数从函数返回,您的 C++ 代码没有填充该参数。这就是您的 C++ 代码失败的原因。参见 Why can a WideString not be used as a function return value for interop? . C++ 端的正确函数签名必须看起来更像这样:
typedef void (__stdcall* GetLastErrText_funtype)(WideString&);
但是,WideString
是 BSTR
的包装器,它本身并不是实际 BSTR。您不能在 Delphi 和 C++Builder 编译器之外按原样使用 WideString
。
您需要重写 DLL 函数,使其更兼容非 C++Builder 编译器。例如:
function GetLastErrText: PWideChar; stdcall;
var
RealResult: WideString absolute Result;
begin
try
Initialize(RealResult);
RealResult := 'This is the result of GetLastErrText';
except
on E: Exception do
ShowMessage('Delphi exception::GetLastErrText : ' + E.Message);
end;
end;
或者:
function GetLastErrText: PWideChar; stdcall;
begin
try
Result := nil;
WideString(Result) := 'This is the result of GetLastErrText';
except
on E: Exception do
ShowMessage('Delphi exception::GetLastErrText : ' + E.Message);
end;
end;
关于c++ - 从 C++ 调用 Delphi DLL 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56955088/
我有一个 Windows Service(一个发布版本),我替换了一个在 Debug模式下构建的 DLL 并尝试启动服务。它引发了错误无法加载文件或程序集“名称”或其依赖项之一。尝试加载格式不正确的程
我使用 Microsoft Visual Basic 6.0 Enterprise Edition SP6 开发了一个 VB6 应用程序。 我已将可执行文件复制到未安装 VB6 的计算机 C。 我已在
我有很多小的 DLL,我想将它们组合成一个大的(更)DLL(如 suggested here)。我可以通过合并我的项目来做到这一点,但我想要一种不那么干扰的方式。 可以将多个 DLL 合并为一个单元吗
我想将非托管 DLL 和图像文件与托管 DLL 合并。我该怎么做?可能吗? 最佳答案 很有可能。这可以通过 bxilmerge 来完成此解决方案可以将非托管 DLL 和图像或其他文件与托管 DLL 合
如何在Windows Server 2003上安装msvcr71.dll,这是我的软件所需的。我真的不想将此dll复制到system32文件夹,因为它可能会破坏此目标系统。 最佳答案 只需将其复制到程
我最近遇到了一个安装在我的系统上的 DLL,Dependancy Walker(以及我尝试过的所有其他实用程序)说按名称或顺序导出为零,但文件大小约为 4mb。我认为 DLL 的唯一目的是导出供其他代
我终于让我的本地主机在本地显示该站点。一切似乎都在朝着这个方向努力。我的下一步是当网站使用 ActiveX.dll 中的函数时,实际上能够从 VB6 IDE 进入代码 更新: 我更新了代码并删除了我在
首先,我指的是Windows环境和VC++编译器。 我想要做的是重建一个Vc++ dll并与已经链接到该lib的exe保持兼容性,而不必重建该exe或使用LoadLibrary动态加载该dll。换句话
我以前这样做过,我不记得我是从网上下载的 DLL 还是其他东西,但我不想感染病毒。我需要访问这个命名空间,以便我可以拥有 Webbrowswer 控件不提供的额外功能。 如何准确添加 Com 引用。还
我正在尝试为依赖于 ghostscript 的库创建 Nuget 包,因此引用 gsdll32.dll - 一个非托管库。我不能只包含一个标准的 dll 引用。我应该把它放在 nuget 目录结构中的
如果我有 Windows 可执行文件,我如何找出它将加载哪些 dll? 我只是谈论哪些将静态加载,而不是那些可能使用 LoadLibrary 等动态加载的内容。 最佳答案 dumpbin是VC++自带
这可能是一个非常菜鸟的问题,但在当今的网络应用程序开发世界中,许多程序员不需要过多处理 dll,因此也懒得去了解它们的用途。 那么什么是dll? 它有什么用? 它是如何工作的? 如何创建一个? 在什么
所以我刚刚开始使用 OpenTK,并将此代码放在一个继承 GameWindow 类的类中: protected override void OnRenderFrame(FrameEventArgs e
如何调试未由 java 应用程序加载的 dll。 场景是这样的:我的 java 应用正在加载正在使用另一个 dll 的 jni.dll,而那个 dll 正在使用另一个 dll。 javajni.dll
我将“更好”放在引号中,因为这是一个定性问题。几年来我一直在编写 COM DLL,直到最近才发现并成功使用了带有 Typelib 概念的标准 DLL。 使用 COM DLL 代替 DLL+Typeli
这种用户机器可能没有msvcp100.dll、msvcp100.dll之类dll的情况怎么处理?我不希望我的软件因为这种错误而无法安装在用户的机器上。我一直在考虑要么找到一个工具并将每个需要的 dll
在 VS2012 中使用 C# .NET 和 COM 互操作,我正在开发一个用于其他几个程序的公共(public)库。为了简化集成,我想将整个库缩减为一个 DLL。该库的特点之一是本地化。它有包含多种
我在 .Net 中使用 FieldTalk Modbus。当我运行应用程序时,我在为 MbusTcpMasterProtocol 创建对象时遇到错误。 MbusTcpMasterProtocol mb
我想指出,我知道如何向/从 GAC 添加/删除程序集。我要问的是,是否有人可以从技术角度向我解释它是如何工作的。将 dll 放在那里有什么意义 - 从节省资源的角度来看。 亲切的问候 最佳答案 将东西
我继承了一个传统的经典 ASP 应用程序,它使用 VB6 ActiveX DLL 来执行业务逻辑。 我想跟踪加载和卸载 DLL 的点。有没有办法在 VB6 DLL 中拦截这些事件? 在相关说明中,Cl
我是一名优秀的程序员,十分优秀!