DLL周围的Win32 DLL包装器(C#类库) [英] Win32 DLL Wrapper around DLL (C# class library)

查看:290
本文介绍了DLL周围的Win32 DLL包装器(C#类库)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用C#开发的托管类库.
现在,我需要围绕它创建一个win 32 dll包装器.
因此,在Win 32 DLL中,我需要导入该类库并从中调用起点函数.
我是新来的.我能够创建函数并在测试代码中使用它.

I have a managed class library developed in C#.
Now I need to create a win 32 dll wrapper around it.
So in my Win 32 DLL i need to import that class library and call the starting point functions from it.
I am new on this. I was able to create functions and use it in test code.

int _stdcall sum(int x , int y)
{
    return x + y;
}
char* _stdcall to_upper(char *lowerstring)
{
    _strupr(lowerstring);
    return lowerstring;
}



但是我需要调用外部dll(C#类库)并从Win32 DLL中创建的函数中调用函数.
我们将不胜感激.



But I need to call external dll (C# class library) and call functions from the functions created in Win32 DLL.
Help would really be appreciated.

推荐答案

有两种方法可以做到这一点:

There are two ways to do this:


  1. 使用C ++/CLI访问C#代码.这意味着您的本机DLL将变为混合模式,这可能是您不希望的.不过,您可以将它们拆分,拥有一个包装C#代码并在本地公开的C ++/CLI DLL,您的Win32 DLL可以通过本机接口访问它,然后可以添加任何您想要的额外内容.


  1. Use C++/CLI to access the C# code. This would mean your native DLL would become mixed mode, which you may not desire. You could split them up though, have a C++/CLI DLL that wraps the C# code and exposes it natively, and your Win32 DLL can access it via the native interface and then you can add whatever extra stuff you want.


使用COM互操作从Win32 DLL访问C#代码.这意味着您至少要从客户端的角度,要对使用COM有所了解.


Use COM interop to access the C# code from your Win32 DLL. This would mean you''d need to have a decent grasp of using COM, at least from a client perspective.



警告:如果您不熟悉这一切,请花一周的时间来熟悉C ++/CLI和/或COM基础知识,然后再开始.


~~~~~~~

根据您的评论要求提供链接:

这是我的C ++/CLI书中的示例章节.请参见 4.2.1从本机代码访问托管库.

http://www.manning.com/sivakumar/sample-ch04_sivakumar.pdf [ ^ ]



Word of warning: If you are very new to all this, take a week to get yourself familiar with C++/CLI and/or COM fundamentals before you start on this.


~~~~~~~

In response to your comment asking for a link:

Here''s a sample chapter from my C++/CLI book. See section 4.2.1 Accessing a managed library from native code.

http://www.manning.com/sivakumar/sample-ch04_sivakumar.pdf[^]


又一个问题:

在我的类库中,我已将char *定义为参数.
现在在Win32 Dll中调用该函数后,我得到如下错误:

类库:
不安全的int Shk(char * lF,char * sF,int ver)
Win32 Dll:
pSGD-> Shk(lF,sF,ver,&lResult)

无法将参数从"char *"转换为"unsigned short *"

有没有一种方法可以通过转换char *-> gt来直接传递字符串?字符串.
或者我可以在调用函数中将char *作为参数传递.

我们将不胜感激.
One more problem:

In my class library i have defined char* as parameters.
Now in Win32 Dll after calling that function i am getting error as:

Class Lib:
unsafe int Shk(char* lF, char* sF, int ver)
Win32 Dll:
pSGD->Shk(lF, sF, ver, &lResult)

cannot convert parameter from ''char *'' to ''unsigned short *''

Is there a way that i can directly pass string by converting char* -> string.
Or can i pass char* as parameter in calling function.

Help would be appreciated.


这是另一种根本不同的方法:直接从.NET Assembly导出到非托管.许多人会说这是不可能的,但事实并非如此:CLR标准允许这样做,并且绝对是明智和合理的.这种方法非常精简,我认为比Nishant正确指出的其他两种方法要好.

您将在我的其他答案中找到有关此想法的解释和一组不错的参考资料: loading MFC中的C#DLL [ ^ ]

—SA
This is another one, radically different approach: direct export from .NET Assembly to unmanaged. Many would say this is not possible, but it''s not true: this is allowed by CLR standard and perfectly sage and legitimate. This approach is very lean and, in my opinion, is better then the other two, correctly pointed out by Nishant.

You will find explanation of the idea and good set of references in my other Answer: loading C# DLL in MFC[^]

—SA


这篇关于DLL周围的Win32 DLL包装器(C#类库)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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