Lib文件C#的托管包装 [英] Managed Wrapper for a Lib File C#

查看:91

一种方法是使用C ++/CLI并像使用C ++一样直接使用非托管DLL.使用C ++/CLI创建公共包装程序托管("ref")类.包装项目应为混合模式(托管+非托管).在输出时,您将获得一个可用作常规.NET库的库.您可以通过任何其他.NET程序集进行引用.

第二种方法是仅使用C#和P/Invoke.您的情况非常简单;怎么可能是个问题?请执行以下操作:

 // 我只显示了实际需要的using子句:
使用 dword =系统. UInt32 ;
使用 DllImportAttribute = System.Runtime.InteropServices.DllImportAttribute;

 MyWrapper {
    常量 字符串 LibraryName = "  MyUnmanaged.DLL";
    
    [DllImport(LibraryName,EntryPoint = " )]
    内部 静态 外部 " )]
    内部 静态 外部 dword BuzzerRegRead(dword RegNum );
} 



命名参数EntryPoint在这里是多余的,但是我建议使用它,因为导出的名称可能会有所不同(名称修改,在这种情况下,您会被"extern C"抑制).最好使用某些二进制转储工具(例如"dumpbin.exe")检查实际导出的内容-在Visual Studio命令提示符中使用它.

参见:
http://msdn.microsoft.com/en-us/library/system. runtime.interopservices.dllimportattribute.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.runtime. interopservices.aspx [ ^ ],
名称修饰,又名名称修饰: http://en.wikipedia.org/wiki/Name_decoration [< ^ ].

—SA


SAKryukov,

谢谢您的回复.我将阅读这些链接.

在第二种选择中,您提到了.DLL,就我而言,我没有DLL.我有一个.Lib我可以以相同的方式使用.Lib文件吗?也许不是.

我找到了一篇文章,解释了如何创建包装器. How to create a DLL library in C and then use it with C#[^] ) but my header file seems a little different to the one used in that example.

I am trying to turn on a buzzer on a CE device. I would like to create a form using C#. I understand I have to create a wrapper but struggling a little with the concepts. The contents of the header file looks like this:

#ifndef __BUZZERIF_H__
#define __BUZZERIF_H__


#if __cplusplus
extern "C" {
#endif

BOOL BuzzerState(DWORD dwState) ;
DWORD BuzzerRegRead(DWORD RegNum) ;

#if __cplusplus
}
#endif

#endif //__BUZZERIF_H__

-----------------------------------------------------------------------


The following C code turns the buzzer "on"

DWORD dwVal ;
dwVal = 0x0001 ;
if ( !BuzzerState(dwVal) )
{
printf("BuzzerState() FAILED\r\n") ;
}
else
{
printf("Buzzer state set to = %u \r\n", dwVal) ;
}





If anyone could provide me with pointers as to how I can create the wrapper so that i can set/call the function in C# (or even the simplest example code), that would be truly appreciated.

解决方案

One way is using C++/CLI and use your unmanaged DLL directly as it is done if C++. Using C++/CLI, create a public wrapper managed ("ref") class. You wrapper project should be mixed-mode (managed+unmanaged). On output, you get a library which can be uses as a normal .NET library. You can reference by any other .NET Assembly.

The second way is using C# only and P/Invoke. Your case is extremely simple; how come it could be a problem? Do this:

//I show only actually needed using clauses:
using dword = System.UInt32;
using DllImportAttribute = System.Runtime.InteropServices.DllImportAttribute;

class MyWrapper {
    const string LibraryName = "MyUnmanaged.DLL";
    
    [DllImport(LibraryName, EntryPoint = "BuzzerState")] 
    internal static extern bool BuzzerState(dword dwState);

    [DllImport(LibraryName, EntryPoint = "BuzzerRegRead")] 
    internal static extern dword BuzzerRegRead(dword RegNum);
}



The named parameter EntryPoint is redundant here, but I recommend to use it because the exported name could be different (name mangling, which is your case is suppressed by "extern C"). It''s best to check up what is actually exported using some binary dump tool such as "dumpbin.exe" — use it from Visual Studio Command Prompt.

See:
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.aspx[^],
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.aspx[^],
Name mangling, aka name decoration: http://en.wikipedia.org/wiki/Name_decoration[^].

—SA


Hi SAKryukov,

thank you for your reply. I will read up on those links.

In your second option, you have mentioned a .DLL, in my case I don''t have a DLL. I have a .Lib Can I use the .Lib file in the same way ? Perhaps not.

I found an article that explains how to create the wrapper. http://tom-shelton.net/index.php/2008/12/11/creating-a-managed-wrapper-for-a-lib-file/comment-page-1/#comment-108[^]

But I am stuck on step 6 because it looks different to my header file. My apologies if this is a silly problem, I am still learning all this programming stuff.

Anybody know what I can do in step six for the header file I have listed?


J


这篇关于Lib文件C#的托管包装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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