Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to Call JNA to VB.net ByRef String Function #25

Closed
K-Full opened this issue Jan 26, 2017 · 2 comments
Closed

How to Call JNA to VB.net ByRef String Function #25

K-Full opened this issue Jan 26, 2017 · 2 comments
Labels

Comments

@K-Full
Copy link

K-Full commented Jan 26, 2017

I want to call vb.net ByRef and String function from java.
vb.net dll is maked by UnmanagedExports.

//java
String[] str = {"testJavaWrited"};
String[] str2 = {"testJavaWrited2"};
final VbWrapper wr = (VbWrapper)Native.loadLibrary("vbtestlib", VbWrapper.class);
wr.test(str,str2);
System.out.println(str); // not printed
System.out.println(str2); // not printed

// jna mapping
public interface VbWrapper extends Library {
public int test(String[] str,String[] str2);
}

//vb.net
<DllExport("test", CallingConvention.StdCall)>
Function test(ByRef str As String, ByRef str2 As String) As Integer
MsgBox("test Called") ' printed
MsgBox(str) ' printed
str = "testStringWrited"
str2 = "testStringWrited2"
Return 0
End Function ' heap error

I need your help everybody.

http://stackoverflow.com/questions/41830352/how-to-call-jna-to-vb-net-byref-string-function

@3F
Copy link
Owner

3F commented Jan 27, 2017

:) You should use pointer to allocated string.

For example:

[DllExport]
public static int getStringArgs(ref IntPtr ptr)
{
    ptr = new UnmanagedString("Hello from .NET and Conari", UnmanagedString.SType.Ansi);    
    return 0;
}

[DllExport]
public static IntPtr getString()
{
    return new UnmanagedString("Hello from .NET and Conari", UnmanagedString.SType.Ansi);
}

Java:

public interface CAPI extends Library
{
    public PointerByReference getString();
    public int getStringArgs(LongByReference ptr); // see also Pointer etc.
}
CAPI l = Native.loadLibrary("DllExportAndJNA", CAPI.class);
...
LongByReference ptr = new LongByReference();
l.getStringArgs(ptr);
(new Pointer(ptr.getValue())).getString(0); // Hello from .NET and Conari
PointerByReference pbr = l.getString();
pbr.getPointer().getString(0); // Hello from .NET and Conari

etc.
In example above, the UnmanagedString is part of Conari engine, but of course you can use anything else, just for sample

anyway, do not forget deallocate all data from unmanaged memory !
And I recommend to use structures to aggregate all your states if you plan complex parameters between calling.

@3F 3F added the question label Jan 27, 2017
@K-Full
Copy link
Author

K-Full commented Jan 30, 2017

Thank you very much 3F.
Your idea is good solution.
But, I wanted to use the standard library as much as possible.

Here is my vb code.

[DllExport]
public static int getStringArgs(ref IntPtr ptr)
{
    ptr = Marshal.StringToCoTaskMemAnsi("Hello from .NET")
    return 0;
}

[DllExport]
public static void freeString(ref IntPtr ptr)
{
    Marshal.ZeroFreeCoTaskMemAnsi(ptr)
    return;
}

Thank you for your help.

@3F 3F closed this as completed Jul 16, 2018
3F referenced this issue Aug 26, 2018
* FIXED: Fixed bug when some methods cannot be exported. Issue #59.
* FIXED: Bug 'The node is not parented by this object' when Configuring projects. Issue: #77.
* FIXED: Fixed GDI objects leak in Wizard.
* FIXED: `-msb` key cannot affect for GetNuTool section. Issue #74.
* FIXED: Bug when automatic restoring still uses default keys from manager after configuring with custom `-server`.
* FIXED: Problem with double quotes for `-packages` key.
* FIXED: Possible incorrect repetition of the relative path for `-packages` key.
* FIXED: Possible problem 'The request was aborted: Could not create SSL/TLS secure channel.'. Issue: #77.
* FIXED: Possible problem with path when `-msb` key contains round brackets,
         e.g.: `D:\C\Program Files (x86)\Microsoft Visual Studio\`...

* NEW: Implemented features for additional automation. Issue #76.
        New actions:
            * `-action Export`
            * `-action Recover`
            * `-action Unset`

        Documentation: https://github.com/3F/DllExport/wiki/DllExport-Manager#automation

* NEW: Added proxy support for manager.
        The common format: `[usr[:pwd]@]host[:port]`

        Documentation: https://github.com/3F/DllExport/wiki/DllExport-Manager#proxy

* NEW: Wizard. Added sorting an projects in main window by its installing status and by availability.
* NEW: Wizard. Added filter for list of projects in main wizard window.
* NEW: New 'Offline' versions from our packages. See GitHub Releases page.
* NEW: Added key to force update `Reference` without PublicKeyToken. Issue #65.
* NEW: Added `-force` key for manager to use aggressive behavior, e.g. like removing pkg when updating.
       Wiki: https://github.com/3F/DllExport/wiki/DllExport-Manager#receiving-new-package-version

* NEW: New action `-action Upgrade`.
       Aggregates an Update action with additions for upgrading.

            Today's an Upgrade action:
            ```
            DllExport -action Upgrade ...
            ```

            is equal to:
            ```
            DllExport -action Update -mgr-up -force ...
            ```

            Wiki: https://github.com/3F/DllExport/wiki/DllExport-Manager#updating

* CHANGED: Case sensitivity for the Action names.
* CHANGED: `-action Default` for nothing.
* CHANGED: UI layout fixes for -Info form (Thanks @Genteure, Issue #61).
* CHANGED: Allows absolute path for `-packages` key.
* CHANGED: `-sln-file` key now can affect to `-action Configure`
* CHANGED: hMSBuild tool now is also distributed inside root directory of the packages zip & nupkg.
           Use this for EXP0014 problem: "RunIlAsm. The library manager still cannot be found."
           Example: https://www.youtube.com/watch?v=zUejJ4vUPGw

* CHANGED: Updated GetNuTool v1.6.2
* CHANGED: Updated MvsSln v2.1.0
* OTHER: Some other fixes and changes with manager and wizard.
* NOTE: To upgrade configured version: `DllExport -action Upgrade ...`
* NOTE: Configuring projects: `DllExport -action Configure ...`
* NOTE: Screencasts:
            * Quick start: https://www.youtube.com/watch?v=sBWt-KdQtoc
            * Basic examples for C++ and C#: https://www.youtube.com/watch?v=9Hyg3_WE9Ks
            * Complex types and Strings: https://www.youtube.com/watch?v=QXMj9-8XJnY

* NOTE: Our actual wiki - https://github.com/3F/DllExport/wiki
        * Documentation for manager: https://github.com/3F/DllExport/wiki/DllExport-Manager
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants