Solved

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

Posted on 2002-04-09
8
217 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 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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now