Solved

How to transfer binary data form an ActiveX control ....

Posted on 2002-04-09
8
235 Views
Last Modified: 2013-11-20
I make a ActiveX control by "MFC ActiveX ControlWizard",now I want to transfer large numbers of binary data from ActiveX control to the caller.I do it like this:
ActiveX control source code:

BOOL CPctCtrl::MyTest(BSTR FAR* szOutBuffer, long nBufferLength)
{
     if(szOutBuffer == NULL)
     {
          return FALSE;
     }

     memcpy(szOutBuffer,"Test",sizeof("Test"));

     return TRUE;
}

I use Visual basic 6.0 to call this ActiveX control like this:

dim szOutBuffer(1024) as string
MyObject1.MyTest szOutBuffer(1),1024
end sub

when run the line "MyObject1.MyTest szOutBuffer(1),1024
" , the program will crashed with "invalid memroy address accessed!"

Cound help me to solve this problem?
Thanks in advance.
0
Comment
Question by:gdzdongdong
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 3

Accepted Solution

by:
job_s earned 100 total points
ID: 6933494
I'm not an expert in VB
one doubt why ur calling szOutBuffer(1)

is szOutBuffer engough ??
MyObject1.MyTest szOutBuffer(1),1024

and why Ur using memcpy( ).
Allocate the BSTR by ::SysAllocStringByteLen( string, size );





BOOL CPctCtrl::MyTest(BSTR FAR* szOutBuffer, long nBufferLength)
{
    if(szOutBuffer == NULL)
    {
         return FALSE;
    }
    if( nBufferLength < strlen(test))
          return false; //buffer small to copy
    *szOutBuffer = ::SysAllocStringByteLen( "test", strlen(test));
    return TRUE;
}

0
 

Author Comment

by:gdzdongdong
ID: 6934022
Thank you for you answer,but my "szOutBuffer" is enough , because I just copy 10 bytes to "szOubBuffer", I think ,I'm in incorrect way ? Could any can help me ???
0
 
LVL 3

Expert Comment

by:job_s
ID: 6934079
If u want to follow ur current method U must add



memcpy(szOutBuffer,"Test",sizeof("Test"));

ZeroMemory( szOutBuffer, nBufferLength);
memcpy(szOutBuffer,"Test",strlen("test"));



try this


   

0
Independent Software Vendors: 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!

 
LVL 3

Expert Comment

by:job_s
ID: 6934497
I think the problem is because of missing the NULL char on the END of string.
0
 

Author Comment

by:gdzdongdong
ID: 6935877
Thank yo job_b, but I already do it like this , memcpy(szOutBuffer,"test",sizeof("test")); but the program still crashed.... Help me???
0
 
LVL 3

Expert Comment

by:job_s
ID: 6936669
I think the problem is because of missing the NULL char on the END of string.
So use
ZeroMemory( szOutBuffer, nBufferLength);
memcpy(szOutBuffer,"Test",strlen("test"));
or use strcpy for copying string





0
 

Author Comment

by:gdzdongdong
ID: 6939439
Thanks everyone , I'll try it ...
0
 

Author Comment

by:gdzdongdong
ID: 6940118
I've tried all the way you have suggested , but the program still crashed ,,, how to do , everyone?
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Finishing the grid ā€“ keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. ā€¦
In this post we will learn different types of Android Layout and some basics of an Android App.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this vā€¦

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question