How to call a dll file from c#

2019-05-22 06:39发布

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.

标签: c# dll ikvm
2条回答
你好瞎i
2楼-- · 2019-05-22 07:12
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

查看更多
Emotional °昔
3楼-- · 2019-05-22 07:28

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.

查看更多
登录 后发表回答