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
Solved

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

Posted on 2013-05-24
13
1,340 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
going to wrong jsp page 2 33
jmss example java 2 23
Setup GlassFish 4 21
Where does legacy ASP.NET initialize language? 2 22
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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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 how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

856 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