Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2002-04-09
8
Medium Priority
?
238 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
  • 4
  • 4
8 Comments
 
LVL 3

Accepted Solution

by:
job_s earned 200 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
Technology Partners: 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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

877 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