Sharing data to separate VC++ process from Excel VBA

A

arothberg

I have a legacy application using a VC++ application and Excel VBA.
The summary of the application flow is as follows:

Excel VBA code would retrieve a needed Long value from within the active
sheet and pass it to a VC++ application via a PostMessage API call to the
VC++ application's main window, passing the Long value as the lParam.

Now I need to change the implementation to pass a string. I am constrainted
to have to continue to use PostMessage to communicate with the VC++
application. I tried using GlobalAlloc() in the Excel VBA code where the
string would be stored in the heap (as a byte array) and then the HGLOBAL
would be passed as the lParam. However, it appears to be garbage when I try
to retrieve the data (using GlobalLock()) in the VC++ application.

I'm trying to avoid having to use a physical file (i.e. with the file name
being an integer passed as an lParam).

Any other options to get this string over to the VC++ application where I'm
constrainted to only pass a long via an lParam in PostMessage. (I cannot use
SendMessage).

Thanks in advance.
 
S

Scott McPhillips [MVP]

arothberg said:
I have a legacy application using a VC++ application and Excel VBA.
The summary of the application flow is as follows:

Excel VBA code would retrieve a needed Long value from within the active
sheet and pass it to a VC++ application via a PostMessage API call to the
VC++ application's main window, passing the Long value as the lParam.

Now I need to change the implementation to pass a string. I am constrainted
to have to continue to use PostMessage to communicate with the VC++
application. I tried using GlobalAlloc() in the Excel VBA code where the
string would be stored in the heap (as a byte array) and then the HGLOBAL
would be passed as the lParam. However, it appears to be garbage when I try
to retrieve the data (using GlobalLock()) in the VC++ application.

I'm trying to avoid having to use a physical file (i.e. with the file name
being an integer passed as an lParam).

Any other options to get this string over to the VC++ application where I'm
constrainted to only pass a long via an lParam in PostMessage. (I cannot use
SendMessage).

Thanks in advance.

If your string won't fit in the message parameters a really crude but
easy solution might be to send multiple sequential messages, 8
characters per message.

Another possible solution (I don't know if VBA can do this) would be to
establish a named shared memory section to hold the string. This would
make the memory accessible from both programs. The APIs to use are
CreateFileMapping and MapViewOfFile. Despite the API names, you don't
really need a file: You pass in INVALID_HANDLE_VALUE and it uses a
portion of the paging file. Both applications would call these APIs
using the same unique name (that you make up) for the memory section.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top