Solved

a simple JNA example that invokes a simple C#.4.5 dll having a one function

Posted on 2013-05-24
13
1,368 Views
Last Modified: 2013-06-16
Hi there;

I am trying to call a function in a simple DLL having one function in it. The function takes 2 parameters and returns one.

I find out it is easier in JNA but don't know how to do.

Any example code?

Regards.

P.S. Should I register the DLL to Windows? or any other set?
0
Comment
Question by:jazzIIIlove
  • 6
  • 6
13 Comments
 
LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 39195570
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39195580
Can you show any header file or other source?
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 39196047
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 39196352
lHHere is the dlll content in c#:
I don't see any dll definition - just some C# code. If you're turning that into a native dll then you need to post the result definition, i.e. the exported function prototype. I have no idea why you'd want a dll though - at a quick glance all the current C# code is doing could be done in pure Java...
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 39196561
Hi Cehj;

Yes you are right. It's easily doable in Java but I want to learn how to use a native DLL.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Copy
{
    public class Operation
    {
        public static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
        {
            // Get the subdirectories for the specified directory.
            DirectoryInfo dir = new DirectoryInfo(sourceDirName);
            DirectoryInfo[] dirs = dir.GetDirectories();

            if (!dir.Exists)
            {
                throw new DirectoryNotFoundException(
                    "Source directory does not exist or could not be found: "
                    + sourceDirName);
            }

            // If the destination directory doesn't exist, create it. 
            if (!Directory.Exists(destDirName))
            {
                Directory.CreateDirectory(destDirName);
            }

            // Get the files in the directory and copy them to the new location.
            FileInfo[] files = dir.GetFiles();
            foreach (FileInfo file in files)
            {
                string temppath = Path.Combine(destDirName, file.Name);
                file.CopyTo(temppath, false);
            }

            // If copying subdirectories, copy them and their contents to new location. 
            if (copySubDirs)
            {
                foreach (DirectoryInfo subdir in dirs)
                {
                    string temppath = Path.Combine(destDirName, subdir.Name);
                    DirectoryCopy(subdir.FullName, temppath, copySubDirs);
                }
            }
        }


    }
}

Open in new window


DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs) is signature of the function.

How can i call the dll I generated in Java?

Regards.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39196578
DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs) is signature of the function.
I don't think so. That's the signature of the function in C# i'd gather. What are the fully-qualified types of the parameters? Is that the standard library string class?
http://pages.cs.wisc.edu/~hasti/cs368/CppTutorial/NOTES/STRING.html
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 39196616
>>Is that the standard library string class?
yes.

None of the class definitions above are mine. They are C# defaults. I didn't add use any referencing class.

Regards.

We can if you want simply this to a simple hello world function:
public void HelloWorld(string s)

Suppose that it will take the input and output the string content by invoking the dll from Java.

Regards.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39196725
>>Is that the standard library string class?
yes.

None of the class definitions above are mine. They are C# defaults. I didn't add use any referencing class.

Regards.
I don't think you quite grasped the point. 'string' is a C# class. dlls do not contain C# types, they contain native types. When you build the C# target as type library, you will probably find that those string types are converted to char* or const char*. The latter maps to java.lang.String in JNA. See the following about what happens to type when you compile a C# file to a target type of library:
http://www.codeproject.com/Articles/556582/UsingplusC-23plusfromplusnativeplusC-2b-2bpluswith

And see the following about how to call the resulting dll:

https://jna.java.net/javadoc/overview-summary.html
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 39196993
Hi;

Thanks so what you say is that a managed dll cannot be enough solely but it must be wrapped via C++ function and then can be utilized. Right?

Regards.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39197079
No - i'm saying it becomes native code. If you were using JNI, you would need to write another dll to call its functions normally. Since you're using JNA, you'll be calling a native dll directly. That should take care of marshalling your Java String type to const char*
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 39207303
>> Since you're using JNA, you'll be calling a native dll directly
Is native dll indicates C/C++ implementation?

Regards.
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 39207306
I am lost can you use arrows like?

native.dll->JNA
....->...

and so forth?

Regards.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 39207821
Is native dll indicates C/C++ implementation?
Usually, but not necessarily

I am lost can you use arrows like?
Not quite sure what you're asking, but if you're talking about the calling sequence, it would be

Java->JNA->DLL
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question