Click here to Skip to main content
16,005,181 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionRelease dlls Pin
nripun3-Nov-05 20:28
nripun3-Nov-05 20:28 
AnswerRe: Release dlls Pin
kakan3-Nov-05 21:07
professionalkakan3-Nov-05 21:07 
Questionserial port programming Pin
mysticlol3-Nov-05 20:04
mysticlol3-Nov-05 20:04 
AnswerRe: serial port programming Pin
kakan3-Nov-05 20:14
professionalkakan3-Nov-05 20:14 
AnswerRe: serial port programming Pin
vikas amin3-Nov-05 20:43
vikas amin3-Nov-05 20:43 
GeneralRe: serial port programming Pin
mysticlol3-Nov-05 20:55
mysticlol3-Nov-05 20:55 
Questionserial port programming Pin
mysticlol3-Nov-05 20:03
mysticlol3-Nov-05 20:03 
QuestionArrays from VC++ to VB using SAFEARRAY Pin
Axonn Echysttas3-Nov-05 19:44
Axonn Echysttas3-Nov-05 19:44 
Hi everybody. I've been working at an article about Inter-Process communication that I also intend to post here when finished ::- ). However, I do have one last issue to solve. A quite difficult issue. I want to pass an array from VC++ to VB. From VB to VC++ I made it work quite easy, but I am having trouble doing it the other way around.

As I learned, Visual Basic works with array using SAFEARRAYs. Now. For example, to call a VB function from a VC++ DLL you gotta do this:

//Using the extNumberProc variable, which is a pointer to the VB function for Numbers, calling<br />
//that function and passing to it the value received as parameter.<br />
void CallVBFunctionForNumber (long lSomeValueToSend)<br />
{<br />
	typedef void (__stdcall *OutsideFunction)(long AValue); //Defining the prototype of the function.<br />
	OutsideFunction FunctionCall; //Creating an instance that will be used to call the function.<br />
	FunctionCall = (OutsideFunction)extNumberProc; //Assigning the address to be used for the call.<br />
	FunctionCall(lSomeValueToSend); //Calling the function with the parameter.<br />
}


extNumberProc is an address which was previously received from VB. To call that function all you gotta do in VC++ is this:

CallVBFunctionForNumber(20);

And now here's the function for calling a function from Visual Basic which should accept an array. First, here's the VB function.

'FUNCTION CALLED FROM THE VC++ DLL.<br />
Public Sub ArrayDemoFunction(ByRef Something As Variant)<br />
 MsgBox Something(2)<br />
End Sub


and the VC++ function

//Using the extArrayProc variable, which is a pointer to the VB function for Arrays, calling<br />
//that function and passing to it a locally created array.<br />
void CallVBFunctionForArray ()<br />
{<br />
	typedef void (__stdcall *OutsideFunction)(VARIANT *AValue); //Same as above, creating a function<br />
	OutsideFunction FunctionCall; //prototype, instantiating it, and linking it to the real VB<br />
	FunctionCall = (OutsideFunction)extArrayProc; //function by doing this cast.<br />
<br />
	VARIANT *pVariant = NULL; //A variant typed pointer.<br />
	SAFEARRAY *sarrVC; //The safe array.<br />
	SAFEARRAYBOUND sabBound = { 10, 0 }; //Its bounds.<br />
<br />
	VariantInit (pVariant); //Initializing.<br />
	pVariant->vt = VT_ARRAY | VT_I4; //Setting the type of the variant.<br />
	sarrVC = SafeArrayCreate(VT_I4, 1, &sabBound); //Creating the safe array.<br />
	for (long iCounter = 0; iCounter < 10; iCounter++) //Inserting data in the array.<br />
		SafeArrayPutElement(sarrVC, &iCounter, &iCounter);<br />
  pVariant->parray = sarrVC; //Setting the data of the variant.<br />
	FunctionCall(pVariant); //Calling the VB function (crashes).<br />
}


Executing the above VC++ function works, until calling the VB function. When that happens, everything comes crashing down with an "Unhandled exception in Testapp.exe (OLEAUT32.DLL): 0xC00000005: Access Violation.".

For your reference, here's an article which I used to create the code:

http://www.microsoft.com/msj/0599/wicked/wicked0599.aspx[^]

So... ideas anybody? ::- )

-= E C H Y S T T A S =-
The Greater Mind Balance

-- modified at 1:44 Friday 4th November, 2005
AnswerRe: Arrays from VC++ to VB using SAFEARRAY Pin
JonEngle4-Nov-05 10:47
JonEngle4-Nov-05 10:47 
QuestionWhat's the meaning of &quot;USES_CONVERSION&quot; Pin
chenxiujie3-Nov-05 19:23
chenxiujie3-Nov-05 19:23 
AnswerRe: What's the meaning of &amp;quot;USES_CONVERSION&amp;quot; Pin
Rob Caldecott3-Nov-05 20:37
Rob Caldecott3-Nov-05 20:37 
GeneralRe: What's the meaning of &amp;amp;quot;USES_CONVERSION&amp;amp;quot; Pin
chenxiujie3-Nov-05 20:55
chenxiujie3-Nov-05 20:55 
QuestionHow to process the msg when clicking outside a DoModal-Dialog? Pin
followait3-Nov-05 19:13
followait3-Nov-05 19:13 
AnswerRe: How to process the msg when clicking outside a DoModal-Dialog? Pin
GflPower3-Nov-05 19:41
GflPower3-Nov-05 19:41 
GeneralRe: How to process the msg when clicking outside a DoModal-Dialog? Pin
followait3-Nov-05 20:07
followait3-Nov-05 20:07 
AnswerRe: How to process the msg when clicking outside a DoModal-Dialog? Pin
BadKarma3-Nov-05 22:28
BadKarma3-Nov-05 22:28 
GeneralRe: How to process the msg when clicking outside a DoModal-Dialog? Pin
followait4-Nov-05 1:28
followait4-Nov-05 1:28 
GeneralRe: How to process the msg when clicking outside a DoModal-Dialog? Pin
BadKarma4-Nov-05 6:57
BadKarma4-Nov-05 6:57 
GeneralRe: How to process the msg when clicking outside a DoModal-Dialog? Pin
followait4-Nov-05 16:27
followait4-Nov-05 16:27 
GeneralRe: How to process the msg when clicking outside a DoModal-Dialog? Pin
followait6-Nov-05 2:21
followait6-Nov-05 2:21 
GeneralRe: How to process the msg when clicking outside a DoModal-Dialog? Pin
followait13-Nov-05 2:08
followait13-Nov-05 2:08 
QuestionGet Temporary Internet Files amount of disk space to use variable Pin
Andrei B3-Nov-05 18:48
Andrei B3-Nov-05 18:48 
AnswerRe: Get Temporary Internet Files amount of disk space to use variable Pin
vikas amin4-Nov-05 2:18
vikas amin4-Nov-05 2:18 
QuestionHow to show file properties dialog?and delete to recycle bin dialog(Confirm file delete dlg)? Pin
Tcpip20053-Nov-05 17:59
Tcpip20053-Nov-05 17:59 
AnswerRe: How to show file properties dialog?and delete to recycle bin dialog(Confirm file delete dlg)? Pin
GflPower3-Nov-05 19:15
GflPower3-Nov-05 19:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.