[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2013-05-24
13
Medium Priority
?
1,548 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 2000 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

649 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