|
-
May 22nd, 2012, 09:34 AM
#1
Thread Starter
Member
[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!
-
May 22nd, 2012, 10:01 AM
#2
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.
-
May 22nd, 2012, 10:20 AM
#3
Thread Starter
Member
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.
-
May 22nd, 2012, 10:28 AM
#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)
-
May 22nd, 2012, 04:49 PM
#5
Re: How to call C-Dll with char[] signature
You can simply do this:-
vbnet Code:
' <StructLayout(LayoutKind.Sequential)> _ Public Structure FixedString256 <MarshalAs(UnmanagedType.ByValTStr, sizeconst:=256)> _ Public val1 As String Public Shared Widening Operator CType(ByVal s As String) As FixedString256 Dim ret As FixedString256 ret.val1 = s Return ret End Operator Public Shared Widening Operator CType(ByVal s As FixedString256) As String Return s.val1 End Operator End Structure <DllImport("Val.Dll")> _ Public Shared Function getValue(ByRef val1 As FixedString256) As Integer 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.
-
May 23rd, 2012, 03:28 AM
#6
Thread Starter
Member
Re: How to call C-Dll with char[] signature
Thanks for your replies,
however - no luck at all.
-
May 23rd, 2012, 04:11 AM
#7
Re: How to call C-Dll with char[] signature
 Originally Posted by keltsch
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.
-
May 23rd, 2012, 06:18 AM
#8
Thread Starter
Member
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?
-
May 23rd, 2012, 10:47 AM
#9
Re: How to call C-Dll with char[] signature
Did you try what I posted in #5 ? That definitely works.
-
May 25th, 2012, 01:20 AM
#10
Thread Starter
Member
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.
-
May 25th, 2012, 03:09 AM
#11
Re: [RESOLVED] How to call C-Dll with char[] signature
Chalk up another victory for multi-threading.
-
May 25th, 2012, 04:20 AM
#12
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|