Solved

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

Posted on 2013-05-24
13
1,266 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now