Results 1 to 12 of 12

Thread: [RESOLVED] How to call C-Dll with char[] signature

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2001
    Posts
    36

    Resolved [RESOLVED] How to call C-Dll with char[] signature

    Hi all,
    I tried the search (forum / google) before, but unfortunately I found no solution.
    From VB.NET I have to call a very old c-dll, the methods have signatures like this:

    Code:
    int __stdcall getNumber();
    int __stdcall getValue(char val1[256]);
    I'm able to call the getNumber Method to retrieve an integer value.
    With the Method getValue I have to retrieve a value into my VB app,
    but everything I tried, I get an access violation within my VB app.

    Using old VB6 the following worked:
    Code:
    Private Declare Function getMessage Lib "val.dll" (ByVal val1 As String) As Long
    ...
    dim sTest as String*256
    ...
    Call getMessage(sTest)
    ...
    In VB I tried the method-declaration with IntPtr, FixedLengthString, Char(), Byte() and so on, but with no success. I now, that char* declarations can be handled with the Stringbuilder, but that seem not to work in this case, as the declaration is a char[256] and no pointer.

    So how to declare the c-method above to be used in VB.NET?

    Any hints are welcome!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to call C-Dll with char[] signature

    That second method should have a parameter of type Char() and a return type of Integer. If it requires an array with 256 elements then you need to specify an upper bound of 255 when you create it in VB.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2001
    Posts
    36

    Re: How to call C-Dll with char[] signature

    Thanks for your reply,
    that is the declaration I tried:

    Code:
    Private Declare Function getValue Lib "val.dll" (ByRef val1 As Char()) As Integer
    ...
    Dim sVal as Char(255)
    Dim iRes as Integer = getValue(sVal)
    ...
    Unfortunately without success, it gives the same access violation.

  4. #4

    Re: How to call C-Dll with char[] signature

    I think you have to declare the array as new:
    Code:
    Dim sVal As Char() = new Char(255)

  5. #5
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: How to call C-Dll with char[] signature

    You can simply do this:-
    vbnet Code:
    1. '
    2.     <StructLayout(LayoutKind.Sequential)> _
    3.     Public Structure FixedString256
    4.  
    5.         <MarshalAs(UnmanagedType.ByValTStr, sizeconst:=256)> _
    6.         Public val1 As String
    7.  
    8.         Public Shared Widening Operator CType(ByVal s As String) As FixedString256
    9.             Dim ret As FixedString256
    10.  
    11.             ret.val1 = s
    12.             Return ret
    13.         End Operator
    14.  
    15.         Public Shared Widening Operator CType(ByVal s As FixedString256) As String
    16.             Return s.val1
    17.         End Operator
    18.  
    19.  
    20.     End Structure
    21.  
    22.     <DllImport("Val.Dll")> _
    23.     Public Shared Function getValue(ByRef val1 As FixedString256) As Integer
    24.     End Function

    FixedString256 can be treated as a normal String in code because I've overriden the CTYPE operator.
    Last edited by Niya; May 22nd, 2012 at 04:55 PM.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  6. #6

    Thread Starter
    Member
    Join Date
    Feb 2001
    Posts
    36

    Re: How to call C-Dll with char[] signature

    Thanks for your replies,
    however - no luck at all.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to call C-Dll with char[] signature

    Quote Originally Posted by keltsch View Post
    Thanks for your reply,
    that is the declaration I tried:

    Code:
    Private Declare Function getValue Lib "val.dll" (ByRef val1 As Char()) As Integer
    ...
    Dim sVal as Char(255)
    Dim iRes as Integer = getValue(sVal)
    ...
    Unfortunately without success, it gives the same access violation.
    Why ByRef? ByVal is the default so unless specified, use ByVal ByRef in managed code implies a pointer in unmanaged code. I obviously can't test it but I would expect this to work:
    Code:
    Private Declare Function getValue Lib "val.dll" (ByVal val1 As Char()) As Integer
    ...
    Dim sVal(255) as Char
    Dim iRes as Integer = getValue(sVal)
    ...
    Note the proper declaration and creation of the Char array.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Member
    Join Date
    Feb 2001
    Posts
    36

    Re: How to call C-Dll with char[] signature

    I tried that out, also no luck.
    Once again, the old style use with vb6 works.

    For additional background information:
    The called c-dll is a wrapper for a JNI caller which intercats with the JAVA runtime.

    Might that perhaps interfere with a different threading model in VB.NET?

  9. #9
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: How to call C-Dll with char[] signature

    Did you try what I posted in #5 ? That definitely works.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  10. #10

    Thread Starter
    Member
    Join Date
    Feb 2001
    Posts
    36

    Re: How to call C-Dll with char[] signature

    Thanks for all your hints - we localized the problem and it unfortunately isn't affected by the call signature but by the dll itself. The called dll uses an internal JVM initialization which fails in the second call as the call to the Dll comes from a different thread. That's why the call fails and lasts in an exception.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] How to call C-Dll with char[] signature

    Chalk up another victory for multi-threading.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Member
    Join Date
    Feb 2001
    Posts
    36

    Re: [RESOLVED] How to call C-Dll with char[] signature

    Yes, that's quite odd!

    Now we ran into another problem, perhaps anyone has an idea in chance?

    The construct is:
    .NET App (MultiThreaded) -> C-DLL (JNI) -> JAVA (in JVM)

    After several tests the Initialization of the JVM only works, if the demanded HeapSpace is reduced to 32MB (!) - a larger size (i.e. 256MB) last in an initialization error (not enough memory on heap). System indicates free memory of 1.5GB - so it's no physical problem. What might be the best way to come around with this?

    * Increasing heap size for unmanaged code called from .NET (I think it isn't possible).
    * Defragmenting heap (I think it's also not possible from .NET at all ).

    I tried figuring out the maximum additional available memory using System.Runtime.MemoryFailPoint() and it fails at 300MB ... so I'm suprised again ... is there any setting (compiler o.e.) to adjust this.
    The System ist WinXP SP3 3GB with /3GB switch on ...

    Thanks again ...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width