-
April 17th, 2012, 12:25 PM
#1
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.
-
April 17th, 2012, 09:39 PM
#2
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)
-
April 18th, 2012, 08:16 AM
#3
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.
-
April 18th, 2012, 11:33 AM
#4
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
with a
safe-array structure to pass arrays to and from the 'C' dll.
-
April 18th, 2012, 02:13 PM
#5
Re: Write 'C' char array[] to VB array() As Char
Well VB6 doesn't know about Pointers, so it might not be possible.
-
April 18th, 2012, 02:18 PM
#6
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.
-
April 18th, 2012, 02:56 PM
#7
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.
-
April 18th, 2012, 05:00 PM
#8
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...
-
April 18th, 2012, 06:44 PM
#9
Re: Write 'C' char array[] to VB array() As Char
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
|