How do I pass a byte array from VC++ to VB?
-
First, my apologies for posting this again - it went unanswered in the VB forum so I am trying with a bigger audience here.... I have a VB.NET 2005 application that makes calls to a C/C++ (VC6) DLL and I have that part working fine. The C/C++ DLL makes use of callback functions to let the VB application know of unsolicited events and that works as well (the functions are at least being called). The problem is I don't know the proper parameters to use (either the C++ or VB side) to get a byte array from C++ back to VB. I have tried the following:
C++ side: BYTE bArray[] = { 11, 12, 13, 14, 15 }; int len = 5; pfnCallback(bArray, len); VB side: CallbackHandler(ByVal data() as Byte, ByVal len as Integer)
This resulted in the data array length of 1 and it only had the first value of the array no matter how big my array was. Then I tried:C++ side: SAFEARRAY *psa; (then properly created psa, and data copied in) pfnCallback(psa); (then properly destroy psa) VB side: CallbackHandler(ByVal data() as Byte)
This resulted in the data array length of 1 and it had the first element of C's SAFEARRAY in it (cDim). I could not see my data. Am I barking up the wrong tree? Is what I want to do possible? I tried ByRef and got exception errors as it jumped from native code to managed code. For the short term, I made a string showing the hexadecimal values of the byte array and passed the string from the C/C++ library back up to VB; that works fine for now.... Thanks in advance, Paul -
First, my apologies for posting this again - it went unanswered in the VB forum so I am trying with a bigger audience here.... I have a VB.NET 2005 application that makes calls to a C/C++ (VC6) DLL and I have that part working fine. The C/C++ DLL makes use of callback functions to let the VB application know of unsolicited events and that works as well (the functions are at least being called). The problem is I don't know the proper parameters to use (either the C++ or VB side) to get a byte array from C++ back to VB. I have tried the following:
C++ side: BYTE bArray[] = { 11, 12, 13, 14, 15 }; int len = 5; pfnCallback(bArray, len); VB side: CallbackHandler(ByVal data() as Byte, ByVal len as Integer)
This resulted in the data array length of 1 and it only had the first value of the array no matter how big my array was. Then I tried:C++ side: SAFEARRAY *psa; (then properly created psa, and data copied in) pfnCallback(psa); (then properly destroy psa) VB side: CallbackHandler(ByVal data() as Byte)
This resulted in the data array length of 1 and it had the first element of C's SAFEARRAY in it (cDim). I could not see my data. Am I barking up the wrong tree? Is what I want to do possible? I tried ByRef and got exception errors as it jumped from native code to managed code. For the short term, I made a string showing the hexadecimal values of the byte array and passed the string from the C/C++ library back up to VB; that works fine for now.... Thanks in advance, Paulhave you tried using an IntPtr on the VB side ? CallbackHandler(ByVal dataPtr as IntPtr, ByVal len as Integer) then, Marshall the data into your VB objects...
image processing toolkits | batch image processing | blogging
-
First, my apologies for posting this again - it went unanswered in the VB forum so I am trying with a bigger audience here.... I have a VB.NET 2005 application that makes calls to a C/C++ (VC6) DLL and I have that part working fine. The C/C++ DLL makes use of callback functions to let the VB application know of unsolicited events and that works as well (the functions are at least being called). The problem is I don't know the proper parameters to use (either the C++ or VB side) to get a byte array from C++ back to VB. I have tried the following:
C++ side: BYTE bArray[] = { 11, 12, 13, 14, 15 }; int len = 5; pfnCallback(bArray, len); VB side: CallbackHandler(ByVal data() as Byte, ByVal len as Integer)
This resulted in the data array length of 1 and it only had the first value of the array no matter how big my array was. Then I tried:C++ side: SAFEARRAY *psa; (then properly created psa, and data copied in) pfnCallback(psa); (then properly destroy psa) VB side: CallbackHandler(ByVal data() as Byte)
This resulted in the data array length of 1 and it had the first element of C's SAFEARRAY in it (cDim). I could not see my data. Am I barking up the wrong tree? Is what I want to do possible? I tried ByRef and got exception errors as it jumped from native code to managed code. For the short term, I made a string showing the hexadecimal values of the byte array and passed the string from the C/C++ library back up to VB; that works fine for now.... Thanks in advance, PaulHello, Read the article on MSDN: “How to Pass Arrays between Visual Basic and C”. http://support.microsoft.com/kb/207931 Some more links: 1. "Passing an array from VC++ DLL to VB", by Amol Kakhandki on Code project. http://www.codeproject.com/dll/ctovbarray\_passing.asp 2. http://www.manbu.net/Lib/En/Class5/Sub7/1/23.asp Regards
-
Hello, Read the article on MSDN: “How to Pass Arrays between Visual Basic and C”. http://support.microsoft.com/kb/207931 Some more links: 1. "Passing an array from VC++ DLL to VB", by Amol Kakhandki on Code project. http://www.codeproject.com/dll/ctovbarray\_passing.asp 2. http://www.manbu.net/Lib/En/Class5/Sub7/1/23.asp Regards