How to call a dll file from c#

谁都会走 提交于 2019-12-10 10:43:47

问题


I am trying to call a dll file from c#

The dll file is made from a java application using ikvm and for now all the code does is print hello world.

How do i call the dll file in my c# code and is it possible to create an application in java that will return a boolean value to my c# code?

Thanks for your time.


回答1:


I'm not sure I understand what you're trying to do, so apologies if I'm misreading. IKVM should translate your java code to a .NET dll or executable. After the "translation" you should be able to use the .dll more or less in the same way as you would with a "native" .NET code.

If your java application has a main method that prints "hello world" on the console, you should have converted it to a .NET executable (.exe) and not to a dll. After converting it to a .exe (and assuming you're running it on Microsoft .NET on a windows system) you should just execute it.

As for the second part of your question, you can also create a dll (converted from java) that returns a boolean and consume it from a C# application.

See this tutorial for two examples of (pretty much exactly) what you're doing.




回答2:


using System.Runtime.InteropServices;

You can then use

[DllImport("myjavadll.dll")]

Then add the dll as a reference by right clicking and navigating to it in the reference folder.

EDIT:

Here is a link that calls a C++ dll to C#. You may be able to work it out.

Call another languages DLL

EDIT: I have had issues adding DLLs as references and was forced to add as a resource. I believe this was because I was working with a sys32 dll.

Here is an old post by where I was trying to work out some DLL import errors. Maybe itll be helpful if you encounter some problems.

Old Post



来源:https://stackoverflow.com/questions/6929206/how-to-call-a-dll-file-from-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!