Solved

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

Posted on 2013-05-24
13
1,423 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
13 Comments
 
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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

726 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