[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1231
  • Last Modified:

Passing a Char buffer from vb.net to C

Hey,

    I have a VB program that calls on an older c DLL to fill a 4k in size char buffer.  I was wondering what is the best way to do this...I have made several failed attemps...:)  Not really famillar with the managed vs unmanaged code yet...so i am sure i'm missing something simple:)

dasid is a enumerated number for a file
The unisgned char *buf is returned with the data needed in vb program
int len is the size of char buf you are passing in
int wave is not used i just pass in a empty intptr

C code--------------------------------------------------------------------------------
 EXPORT int das_get_wave(int dasid, unsigned char *buf, int len, int *wave);
VB code-------------------------------------------------------------------------------

        Dim arraywavedata_length As Integer = 4096
        Dim wave As IntPtr
        Dim arraywavedata(4096) As String
        Dim arraywavedata_intptr As IntPtr = Marshal.StringToHGlobalAnsi(arraywavedata(4096))

        Public Declare Function das_get_wave Lib "libdas.dll" (ByVal dasid As Integer, ByVal buf As IntPtr, ByVal len As Integer, ByVal wave As IntPtr) As Integer

my intptr to arraywavedata returns 0...
0
computerfixins
Asked:
computerfixins
3 Solutions
 
aravindtjCommented:
Hai,
 I think that in the C function call das_get_wave gives only the integer value.
 But you are getting the pointer in VB call. So, you may get garbage value. Try to return  the pointer in das_get_wave function.
All the Best.
:)
0
 
newwaresCommented:
Hi,
Maybe you can try P/Invoke. Here is an example in manged c++:
http://www.codeproject.com/managedcpp/pinvoke.asp

But you can convert it to Visual Basic .Net.
0
 
computerfixinsAuthor Commented:
Hey,

Forgot this line of code...should make it easier to read....

C code--------------------------------------------------------------------------------
 EXPORT int das_get_wave(int dasid, unsigned char *buf, int len, int *wave);
VB code-------------------------------------------------------------------------------

        Dim arraywavedata_length As Integer = 4096
        Dim wave As IntPtr
        Dim arraywavedata(4096) As String
        Dim arraywavedata_intptr As IntPtr = Marshal.StringToHGlobalAnsi(arraywavedata(4096))

***       arraywavedata_count = das_get_wave(dasid, arraywavedata_intptr, arraywavedata_length, wave)  ***

VB.mod----------------------------------------------------------------------------------
        Public Declare Function das_get_wave Lib "libdas.dll" (ByVal dasid As Integer, ByVal buf As IntPtr, ByVal len As Integer, ByVal wave As IntPtr) As Integer


0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
drichardsCommented:
Is the result supposed to be a String?  If so, you can use code from the 'Declare' docs at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vastmDeclare.asp (code shown below).  Your code is creating an array of NULL String references and trying to marshal one of the NULL references.  That will not work.  You just need a single String with an allocated buffer, and according to the sample, you don't need to explicitly marshal.   The sample creates a 25 character string so you'll have to change that to your desired length.
-----------------------------------------------------------------------------------------------
Declare Function GetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, ByRef nSize As Integer) As Integer
Function GetUser()
   Dim RetVal As Integer
   Dim UserName As String
   Dim Buffer As String
   Buffer = New String(CChar(" "), 25)
   RetVal = GetUserName(Buffer, 25)
   UserName = Strings.Left(Buffer, InStr(Buffer, Chr(0)) - 1)
   MsgBox(UserName)
End Function
-----------------------------------------------------------------------------------------------

If you really want a buffer of unsigned char, you should create a Byte array and pass a pointer to it.  Otherwise, you get unwanted conversions happening since .NET strings are UNICODE.
0
 
computerfixinsAuthor Commented:
The values that are returned in  ByVal buf As IntPtr are from 0-255 so a buffer of unchar would work....

How do you create a pointer too a byte array?

Do you have too marshal the byte array since your going from managed code to unmanaged>?

Will the .net garbage collectors move my pointer on me>?....

Also any good books that scope going from vb.net to older C dll's....that way i wont keep asking newb questions:)



0
 
computerfixinsAuthor Commented:
Hey,

Found a good write up on pointers and vb.net here

http://www.fawcette.com/Archives/premier/mgznarch/vbpj/2001/10oct01/rt0110/rt0110p.asp

Thanks for pointing me in the right direction...:)
0

Featured Post

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now