用c ++创建本机DLL并从c#中调用它 [英] Create native DLL in c++ and call it from c#

查看:55
本文介绍了用c ++创建本机DLL并从c#中调用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是一个答案,而不是一个问题。我希望它可以帮到你,因为我是这么多年来为了获得这方面的信息而烦恼:


对于其他想要创建测试的人来说c ++ nativedll / c #project遵循

这些说明...


1)创建一个新的''Visual C ++ - Win32控制台项目'' - 称之为''测试''

1b)当向导弹出时,单击''应用程序设置''链接

左边

1c)选择应用类型下的DLL'

1d)点击完成(不用担心,一切都用英文)


2)在test.cpp中你需要在

底部添加以下代码自动生成的东西:


// START


struct MyTestStruct

{

int iSomeNumber;

int iAnotherNumber;

};


externC __declspec(dllexport)的空隙

DoSomethingWithStruct(MyTestStruct * MTS);


空隙DoSomethingWithStruct(MyTestStruct * MTS)

{<无线电通信/>
if(mts!= NULL)

{

mts-> iSomeNumber = 12345;

mts-> iAnotherNumber = mts-> iAnotherNumber + 67890;

}

}


// END

那是'dll排序。正如您所看到的,DoSomethingWithStruct只是将引用结构的成员的值设置为




3)现在文件添加项目新项目''视觉C# - Windows

申请表''

3b)在表单上添加一个按钮 - 将其命名为''button1''

3c)添加按钮单击事件处理程序

3d)将代码粘贴到整个事件处理程序下面''private void

button1_Click(...){...}'':


// START


struct MyTestStruct

{

int iSomeNumber;

int iAnotherNumber;

};


[System.Runtime.InteropServices.DllImport(@" REPLACE

_THIS_WITH_THE_PATH_TO_YOUR_TEST.DLL_FILE\test\Deb ug\test.dll")]

私人静态外部空隙DoSomethingWithStruct(参照MyTestStruct

theStruct); <无线电通信/>

private void button1_Click(object sender,System.EventArgs e)

{

MyTestStruct mts = new MyTestStruct();

DoSomethingWithStruct(ref mts);

}


/ / END


3E)改变的地方说:

REPLACE_THIS_WITH_THE_PATH_TO_YOUR_TEST.DLL_FILE指向您的DLL。

$ b $路径b)将C#Windows应用程序设置为启动项目


5)设置断点,以便可以看到mts确实已被更改


6)运行项目。


无耻地改编自
http://blogs.msdn.com/jonathanswift/...02/780637.aspx ,但

感谢b0b添加了一些重要信息 - 你知道你是谁。


问候,

James Randle。


另请查看乔纳森的后续文章
http://blogs.msdn.com/jonathanswift/...23002900_.aspx

消除了需要在

DllImport属性中指定dll的位置。出色的工作:)。

解决方案

" pigeonrandle" < pi ********** @ hotmail.comwrote in message

news:11 ******************** **@a75g2000cwd.googlegr oups.com ...


extern" C" __declspec(dllexport)void

DoSomethingWithStruct(MyTestStruct * mts);



难道这不会导致名称DoSomethingWithStruct被破坏吗?


Michael




" Michael C" < no **** @ nospam.comwrote in message

news:eQ ************** @ TK2MSFTNGP03.phx.gbl ...


" pigeonrandle" < pi ********** @ hotmail.comwrote in message

news:11 ******************** **@a75g2000cwd.googlegr oups.com ...


> extern" C" __declspec(dllexport)void
DoSomethingWithStruct(MyTestStruct * mts);



难道这不会导致名称DoSomethingWithStruct被破坏吗?



使用externC,编译器只会添加一个前导下划线。我认为

P / Invoke非常聪明,无论如何都能找到出口。其他可能是b $ b的事情是添加A。或U或U。取决于你选择的是CharSet.Unicode还是

不是。


>

Michael



Michael,


我不这么认为(摘自
http://blogs.msdn.com/jonathanswift/ ... 2 / 780637.aspx) ...


使用externC强制编译器使用实际的函数名

(就像在C中一样)。这可以防止我们超载这个功能但是

我们在这个例子中并没有为此烦恼。

在相关的说明中,如果你想检查一个dll来查找在

其他东西,导出的函数名称中,你可以使用Visual Studio命令提示符下的dumpbin命令

。键入dumpin / exports文件名

将列出dll中导出的函数名称。在我们的

简单dll上尝试使用和不使用externC关键字看看

装饰在行动。


....除非你有想要添加的东西?!


干杯,

詹姆斯。


再一次,尊重乔纳森斯威夫特,无论你是谁;)


Michael C写道:


" pigeonrandle" < pi ********** @ hotmail.comwrote in message

news:11 ******************** **@a75g2000cwd.googlegr oups.com ...


extern" C" __declspec(dllexport)void

DoSomethingWithStruct(MyTestStruct * mts);



难道这不会导致名称DoSomethingWithStruct被破坏吗?


Michael


Hi,
This is an answer, not a question. I hope it helps you since i was
messing around for ages trying to get information on this:

For anyone else wanting to create a test c++nativedll/c# project follow
these instructions...

1) Create a new ''Visual C++ - Win32 Console Project'' - call it ''test''
1b) When the wizard pops up, click the ''Application Settings'' link on
the left
1c) Select ''DLL'' under ''Application Type''
1d) Click ''Finish'' (don''t worry, everything remains in English)

2) In test.cpp you will need to ADD the following code at the bottom of
the auto-generated stuff:

//START

struct MyTestStruct
{
int iSomeNumber;
int iAnotherNumber;
};

extern "C" __declspec(dllexport) void
DoSomethingWithStruct(MyTestStruct* mts);

void DoSomethingWithStruct(MyTestStruct* mts)
{
if (mts != NULL)
{
mts->iSomeNumber = 12345;
mts->iAnotherNumber = mts->iAnotherNumber + 67890;
}
}

//END

That''s the dll sorted. As you can see, DoSomethingWithStruct just sets
the value of the referenced structure''s members.

3) Now File Add Project New Project ''Visual C# - Windows
Application''
3b) Add a button to your form - leave it named ''button1''
3c) Add the button click event handler
3d) Paste the code below OVER the entire event handler ''private void
button1_Click( ... ) { ... }'':

//START

struct MyTestStruct
{
int iSomeNumber;
int iAnotherNumber;
};

[System.Runtime.InteropServices.DllImport(@"REPLACE
_THIS_WITH_THE_PATH_TO_YOUR_TEST.DLL_FILE\test\Deb ug\test.dll")]
private static extern void DoSomethingWithStruct(ref MyTestStruct
theStruct);

private void button1_Click(object sender, System.EventArgs e)
{
MyTestStruct mts = new MyTestStruct();
DoSomethingWithStruct(ref mts);
}

//END

3e) change the path where it says
REPLACE_THIS_WITH_THE_PATH_TO_YOUR_TEST.DLL_FILE to point to your dll.

4) Set the C# Windows Application as the startup project

5) Set a breakpoint so you can see that mts has indeed been changed

6) Run the project.

Shamelessly adapted from
http://blogs.msdn.com/jonathanswift/...02/780637.aspx, but
with some vital information added thanks to b0b - you know who you are.

Regards,
James Randle.

Also check out jonathan''s followup article
http://blogs.msdn.com/jonathanswift/...23002900_.aspx
which eliminates the need to specify the location of the dll in the
DllImport attribute. Excellent work :).

解决方案

"pigeonrandle" <pi**********@hotmail.comwrote in message
news:11**********************@a75g2000cwd.googlegr oups.com...

extern "C" __declspec(dllexport) void
DoSomethingWithStruct(MyTestStruct* mts);

Doesn''t this cause the name DoSomethingWithStruct to be mangled?

Michael



"Michael C" <no****@nospam.comwrote in message
news:eQ**************@TK2MSFTNGP03.phx.gbl...

"pigeonrandle" <pi**********@hotmail.comwrote in message
news:11**********************@a75g2000cwd.googlegr oups.com...

>extern "C" __declspec(dllexport) void
DoSomethingWithStruct(MyTestStruct* mts);


Doesn''t this cause the name DoSomethingWithStruct to be mangled?

With extern "C", the compiler will just add a leading underscore. I think
P/Invoke is smart enough to find the export anyway. Other things it might
do is add "A" or "U" depending on whether you selected CharSet.Unicode or
not.

>
Michael



Michael,

I don''t think so (taken from
http://blogs.msdn.com/jonathanswift/...2/780637.aspx)...

Using extern "C" forces the compiler to use the actual function name
(as it would in C). This prevents us from overloading this function but
we''re not bothered about that in this example.
On a related note, if you want to examine a dll to find out, amongst
other things, exported function names, you can use the dumpbin command
from the Visual Studio command prompt. Typing dumpin /exports filename
will list the exported function names from the dll. Try it on our
simple dll with and without the extern "C" keywords to see the
decoration in action.

....unless you have something you''d like to add?!

Cheers,
James.

Again, maximum respect to Jonathan Swift, whoever you are ;)

Michael C wrote:

"pigeonrandle" <pi**********@hotmail.comwrote in message
news:11**********************@a75g2000cwd.googlegr oups.com...

extern "C" __declspec(dllexport) void
DoSomethingWithStruct(MyTestStruct* mts);


Doesn''t this cause the name DoSomethingWithStruct to be mangled?

Michael


这篇关于用c ++创建本机DLL并从c#中调用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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