罗伯特·捷的托管输出 [英] Robert Giesecke's Unmanaged Exports

查看:174
本文介绍了罗伯特·捷的托管输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从vb.net非托管C ++工作得到DLLEXPORT。

I'm trying to get an DllExport from vb.net to unmanaged c++ working.

我使用罗伯特捷的托管输出的使用Visual Studio 2012并试图按照这种非常有用的提示的。我在那里我*的.cpp和* .h文件所在的目录中后生成动作复制从.NET项目中的DLL文件。

I'm using Robert Giesecke's Unmanaged Exports with Visual Studio 2012 and tried to follow this very helpful hints. I copy the dlls from the .Net project by an post build action in the directory where my *.cpp and *.h files reside.

我检查了我的dll与 DUMPBIN / EXPORTS Nugget.Discovery.dll ,它告诉我,有出口:

I checked my dll with dumpbin /EXPORTS Nugget.Discovery.dll and it tells me that there are exports:

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文件

But if I try to import it in the cpp file with

#import "Nugget.Discovery.dll" 
   void StartAnnouncing(int serial);

我得到一个智能感知错误和一个错误后,我试图编译:

I get one IntelliSense error and one error after I try to compile:

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.

知不知道我做错了什么?

Any idea what I'm doing wrong?

最好的问候!    斯特凡

Best regards! Stefan

推荐答案

好了,感谢汉斯帕桑特我来到这个解决方案:

Ok, thanks to Hans Passant I came to this solution:

这是我的code。在管理方:

This is my code on the managed side:

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

有趣的是,我不能叫标有功能&LT; D​​LLEXPORT&GT; 从管理code(即使它们是公共)

Interesting is, that I cannot call functions that are marked with <DllExport> from managed code (even if they are Public).

这是在本机端的code:

And this is the code on the native side:

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);    
}
}

还有没有其他更好的办法呢?

Are there any other better solutions?

的Ciao! 斯特凡

这篇关于罗伯特·捷的托管输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆