罗伯特·捷赛克的非托管出口
Robert Giesecke's Unmanaged Exports
我正在尝试让DllExport从 vb.net 到非托管c ++工作。
我在Visual Studio 2012中使用Robert Giesecke的非托管导出,并尝试遵循这些非常有用的提示。我通过 *.cpp 和 *.h 文件所在的目录中的后期生成操作从 .Net 项目复制 dll。
我用dumpbin /EXPORTS Nugget.Discovery.dll
检查了我的dll,它告诉我有导出:
File Type: DLL
Section contains the following exports for Nugget.Discovery.dll
00000000 characteristics
52554A05 time date stamp Wed Oct 09 14:20:21 2013
0.00 version
0 ordinal base
2 number of functions
2 number of names
ordinal hint RVA name
0 0 0000532E StartAnnouncing
1 1 0000533E StopAnnouncing
Summary
2000 .reloc
4000 .rsrc
2000 .sdata
4000 .text
但是如果我尝试将其导入 cpp 文件中
#import "Nugget.Discovery.dll"
void StartAnnouncing(int serial);
我收到一个智能感知错误和一个错误后尝试编译:
IntelliSense: cannot open source file "Debug/Nugget.Discovery.tlh"
error C1083: Cannot open type library file: 'nugget.discovery.dll': Fehler beim Laden der Typbibliothek/DLL.
知道我做错了什么吗?
此致敬意! 斯特凡
作为 DllExport 的一部分,将生成一个 .lib 文件。您可以使用它来使用普通的C++链接器,而不是 LoadLibrary/GetProcAddress。
从您发布的托管代码开始,在本机端:
extern CALLBACK void StartAnnouncingType(int serial);
extern CALLBACK int TestType(void);
int _tmain(int argc, _TCHAR* argv[])
{
int test = TestPtr();
StartAnnouncingPtr(1);
}
在非托管项目的设置中,将Nugget.Discovery.lib
添加到项目属性:配置属性->链接器->输入。并将 Nugget.Discovery.dll 复制到输出目录。
好的,感谢Hans Passant,我得出了这个解决方案:
这是我在托管端的代码:
Imports System.Runtime.InteropServices
Imports RGiesecke.DllExport
Public NotInheritable Class Beacon
Private Sub New()
End Sub
Private Shared _nuggetAnnouncement As NuggetAnnouncement
' ReSharper disable UnusedMember.Local
''' <remarks>Cannot be called from managed code!</remarks>
<DllExport("StartAnnouncing", CallingConvention.StdCall)>
Private Shared Sub StartAnnouncingNative(serial As Integer)
StartAnnouncing(serial)
End Sub
''' <remarks>Cannot be called from managed code!</remarks>
<DllExport("Test", CallingConvention.StdCall)>
Private Shared Function TestNative() As Integer
Return Test()
End Function
' ReSharper restore UnusedMember.Local
Public Shared Sub StartAnnouncing(serial As Integer)
'do something
End Sub
Public Shared Function Test() As Integer
Return 42
End Function
End Class
有趣的是,我无法从托管代码中调用标有<DllExport>
的函数(即使它们是 Public 的(。
这是本机端的代码:
typedef void (CALLBACK* StartAnnouncingType)(int);
typedef int (CALLBACK* TestType)(void);
int _tmain(int argc, _TCHAR* argv[])
{
HINSTANCE dllHandle = NULL;
StartAnnouncingType StartAnnouncingPtr = NULL;
TestType TestPtr = NULL;
wchar_t dllNameWide[64];
int size = mbstowcs(dllNameWide, "Nugget.Discovery.dll", sizeof(dllNameWide));
dllHandle = LoadLibrary(dllNameWide);
if (NULL != dllHandle)
{
//Get pointer to our function using GetProcAddress:
StartAnnouncingPtr = (StartAnnouncingType)GetProcAddress(dllHandle,"StartAnnouncing");
TestPtr = (TestType)GetProcAddress(dllHandle,"Test");
int test;
if (NULL != TestPtr) test = TestPtr();
int serial = 1;
if (NULL != StartAnnouncingPtr) StartAnnouncingPtr(1);
//Free the library:
FreeLibrary(dllHandle);
}
}
还有其他更好的解决方案吗?
再见!斯特凡
相关文章:
- C++ 如何重用具有多个返回(出口点)的代码块?
- 拨打mono_jit_init时出口1
- 在运行libtorch的模型时获得的结果是不正确的,该模型经过训练并从Pytorch出口
- 使用 bazel 从源代码构建张量流服务遇到错误:C++规则'@org_tensorflow//…'编译失败(出口 4)
- 将所有模块加载到过程中,然后将其出口
- 使用清理代码优化多个出口点
- C XTREE崩溃在出口上,映射/设置迭代器不可用
- OSX MOJAVE -LD:架构X86_64 clang找不到符号:错误:链接器命令失败,出口代码1
- C 错误链接器命令失败了出口代码1(使用-V查看调用)
- 陷阱在控制台上出口
- Xcode C :链接命令失败,出口代码1未找到-lboost_regex
- LD:架构x86_64 clang找不到符号:错误:链接器命令失败,出口代码1(使用-v to See
- CMAKE出口目标具有绝对路径
- C 程序在出口时崩溃,内存中的访问违规
- 在过程出口中打破视觉工作室
- 线程出口上的重生线
- 多态DLL出口
- C /R:clang:错误:Linker命令失败,出口代码1(使用-v查看调用)
- 单位测试多个C 项目和团队的出口结果
- 罗伯特·捷赛克的非托管出口