Solved

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

Posted on 2002-04-09
8
229 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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 163
Expand to include initial dialog with two choices. 9 80
Hibernate methods 2 77
matchUp  challenge 9 115
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

830 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