2

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.

matt b
  • 138,234
  • 66
  • 282
  • 345
rik
  • 1,279
  • 4
  • 20
  • 29

2 Answers2

1
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

Community
  • 1
  • 1
sealz
  • 5,348
  • 5
  • 40
  • 70
  • I believe this should work pelase correct me if I am wrong, I am going to doublecheck on an old project in the meantime – sealz Aug 03 '11 at 15:45
  • how would i call the main method then, essentially as that is what the dll is - a main java method that when called prints hello world – rik Aug 03 '11 at 15:46
  • @rik would that tutorial help with the answer? it shows a call dll method before the static void main. – sealz Aug 03 '11 at 15:48
  • i will have a try - looks complicated (i dont mean that rudely btw!) thanks for taking the time to help, i will have a play around with some examples based on that tutorial. thanks again – rik Aug 03 '11 at 15:49
  • I will keep an eye out and when I have a min to look at some old work Ill edit with anything that might be helpful – sealz Aug 03 '11 at 15:50
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.

Paolo Falabella
  • 24,914
  • 3
  • 72
  • 86
  • the idea was that eventually i would make an app that takes a string and provides me with a boolean value to indicate if it was found or not - that wouldnt be possible with an exe would it? i also couldnt include that in the project could i? – rik Aug 03 '11 at 15:56
  • the tutorial I linked addresses that part too. A .dll usually does not have a "main" method that writes to the console, so if you want to use it that way, you should make it a class with a `public String helloWorld()` that simply returns "hello world" as a string. After turning that to a .NET dll with IKVM you could then reference it in a console application project and call `Console.WriteLine(myConvertedClass.helloWorld());` – Paolo Falabella Aug 03 '11 at 16:03
  • does it have to be a console application? i am using this as an aside to a piece in a winforms application? essentially i want to get a list of data thats available from an internal system but the api is 100% java for this part - i also need to check some other data but that is done using c# - the results are then displayed in a table. simply put i wanted to check the data in java and then return the results in my app to display in a table - am i not going to be able to do this? – rik Aug 03 '11 at 17:53
  • It doesn't have to be a console application (it was just the quickest thing to test a "hello world" type of program). Everything should work just the same from a winform. If the conversion from java to .NET dll with IKVM works, AFAIK you should be able to reference the resulting .NET dll from any type of .NET project. – Paolo Falabella Aug 03 '11 at 18:01