CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2010
    Posts
    75

    Write 'C' char array[] to VB array() As Char

    Hi VB Experts

    Is it possible for a 'C' function to use a pointer to a char array to write to a VB array As Char?

    I have tried this but the C function only fills the first element in the VB array.

    C Code:
    Code:
    EXPORT int __stdcall SocknetRecv (int nSocket, LPSTR *lpBuf, int nSize)
    {
    	int nRecv, nLen;
    	LPTSTR lpPtr;
    
    	for (nLen = nSize, lpPtr = *lpBuf; nLen > 0;)
    	{
    		nRecv = recv(nSocket, lpPtr, nLen, 0);
    		if (nRecv <= 0)
    		{
    			nResult = WSAGetLastError();
    			return nRecv;
    		}
    
    		lpPtr += nRecv;
    		nLen -= nRecv;
    	}
    
    	nResult = 0;
    
    	return nSize - nLen;
    }
    And in VB
    Code:
    Public Declare Function SocknetRecv Lib "SocknetLib.dll" (ByVal nSocket As Integer, ByRef Buffer() As Char, ByVal Size As Integer) As Integer
    
    Dim RData(100) As Char
    'Call it
    nBytes = SocknetRecv(Socket, RData, RData.Length)
    If anyone can help me with this i will be most grateful, I do not know VB very well

    Looking forward to your replies
    Last edited by RipRage; April 17th, 2012 at 12:29 PM.

  2. #2
    Join Date
    Sep 2006
    Posts
    635

    Re: Write 'C' char array[] to VB array() As Char

    I don't sure but if you parameter is a pointer
    you cant try something like :

    Code:
    SocknetRecv(Socket, RData(0), RData.Length)
    where lpBuf and rData would be same memory address, then these are
    going to have same values
    Code:
     SocknetRecv (int nSocket, LPSTR *lpBuf, int nSize)

  3. #3
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Write 'C' char array[] to VB array() As Char

    First Dimming As Char wouldn't work, except if you had a userdefined type Char.
    I'd go about like that:
    Code:
    Dim RData(100) as Byte
    'then you declare the function like that:
    Public Declare Function SocknetRecv Lib "SocknetLib.dll" (ByVal nSocket As Integer, ByRef Buffer As Byte, ByVal Size As Integer) As Integer
    'and the call looks like that:
    nBytes = SocknetRecv(Socket, RData(0), UBound(RData) + 1)
    Explanation: Declaring the parameter ByRef Buffer as Byte makes VB to pass a pointer to the function which points to a byte.
    But when calling the function you actually pass the first byte of the array RData(0) which passes the pointer to it, which is ALSO the pointer to the complete array, since it points to the first element. This is how C expects pointers to arrays.
    I'm not sure what will be passed when declaring Buffer() as an array.

    I have used the above method to pass an array of longs from and to a C function which worked well.

    Come to think about it: "RData.Length" makes me think that you are using VB.Net and are possibly in the wrong forum.
    The above method is definitely working with VB6, and I'm not sure if the declaration as described works in VB.Net, too, but I think it should.
    Last edited by WoF; April 18th, 2012 at 08:30 AM.

  4. #4
    Join Date
    Jan 2010
    Posts
    75

    Re: Write 'C' char array[] to VB array() As Char

    Hi Wof

    Thank you for your reply, sadly it didn't work, recv() failed with invalid pointer address.

    I think i will have to replace
    Code:
    LPSTR *lpBuf
    with a
    Code:
    SAFEARRAY *ppSafe
    safe-array structure to pass arrays to and from the 'C' dll.

  5. #5
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Write 'C' char array[] to VB array() As Char

    Well VB6 doesn't know about Pointers, so it might not be possible.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  6. #6
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Write 'C' char array[] to VB array() As Char

    Could you please clarify if you are using VB6 or VB.Net? Because the method of producing a pointer to an array as I described it works allright under VB6.

  7. #7
    Join Date
    Jan 2010
    Posts
    75

    Re: Write 'C' char array[] to VB array() As Char

    Ah yes i do apologize it is VB.Net and also apologize for posting this in the wrong forum. I am a C programmer who is writing a dll for a friend to be used in a 'VB.Net' application. My knowledge on VB is limited.

  8. #8
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Write 'C' char array[] to VB array() As Char

    Well, then you'd better repeat your question in the .Net forum.
    I'm quite sure there is a proper way of declaring the parametrs that would work under VB.Net as well.
    Although, I cannot think why it shouldn't work as I described...

  9. #9
    Join Date
    Jan 2010
    Posts
    75

    Re: Write 'C' char array[] to VB array() As Char

    Thanks you, will do

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

Featured