Solved

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

Posted on 2002-04-09
8
224 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unix / Linux grid computing 5 157
Whole sheet autoscrub still needed 19 50
string initialization in java 11 113
firstChar challenge 13 115
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 …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

772 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