Link to home
Start Free TrialLog in
Avatar of richtelieu88
richtelieu88Flag for United States of America

asked on

Can't Pass Structure from VBA to C COM DLL

I am trying to pass data from VBA to a C COM DLL.  The data will consist of an arra structure
TO-EXPERTS-EXCHANGE.txt
Avatar of richtelieu88
richtelieu88
Flag of United States of America image

ASKER

Sorry, I must have pressed the wrong button before I was finished with my problem description.

Iam trying to pass data from VBA to a C COM DLL.  The data will consist of an array of structures.  In this case, the attached file indicates that the size of the array is 3 structure elements.

I have no difficulty passing arrays to/from VBA / C COM DLL, but passing structures is not intuitive to me.

The attached file contains my latest attempts.
Avatar of evilrix
In general, the way you pass structures between two different languages is to marshal the members of the structure where there are type in one language that can be mapped to the other. In other words, you can't just send a structure across the API boundaries, you'll have to serialize it in some way. Since you are using COM you will need to map the member types of the structure to appropriate native COM types.
evil, do you have a quick example of how to do this marshaling . . . based on the TXT file I attached
granted that the structure has only c types and no pointers and granted that the c com dll is responsible for filling the data, you could spare the serializing from vba to native structure and only implement the deserializing, means, to get the data to vba from buffer.

you would find out the maximum size of the array at the c side and provide an empty byte buffer of same size at the vba side. the c side then could make an appropriate cast to that buffer and fill it. after return you would have to "deserialize" the buffer as evilrix has mentioned. you would "divide" the buffer to get the structure "records". then for each record you have to parse the members and fill an appropriate equivalent vba structure.  

the following article would give sample code how you could pass the buffer (or records) to the com dll:
http://vcfaq.mvps.org/com/4.htm

Sara
Sarabande,  A lot of what you said went in one ear and out the other . . .. but I will review the HTM article.

I *think* what you're getting at is to have the Client and Server both agree on the number *bytes* that will be passed; the Client will pass that number of bytes in one big byte array and the Server will decode that array based on the number of bytes they've both agreed to. . . . . .am I close?  or still far away in understanding???
Sarabande, that artical appears promising.  Let me experiment with it before I award points.
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sarabande,

Well, I followed the instructions, to the letter of the "law", in that article you sent me "http://vcfaq.mvps.org/com/4.htm" and I get the following error in VBA at the line:

scd.SendStruct  st

(Error = "Compile Error:  ByRef argument type mismatch".)

The C COM DLL compiles just fine, but VBA complains.

There's got to be a straightforward, simple, way that works that allows passing a structure pointer FROM VBA TO C COM DLL.  It can't be that complicated.

I'll study the "int some_c_function(unsigned char * buffer . . ." code you sent.

But VBA (or VB) does not allow the "ByRef" in the caller, only in the Callee.  This is beginning to get frustrating.

I'll next study
Sarabande,

I have implemented "int some_c_function()".  It compiles just fine in the C COM DLL.

When I call this in VBA:

scd.GenericStruct  2,1

I get this error message:

"Function or interface marked as restricted, or the function uses an Automation type not supported in Visual Basic".

Does anyone have any simple VBA / C COM DLL examples that actually compile and run . . .??????
I just got this to work:

VBA
scd.GenericStruct 2


C COM DLL
[id(7)] HRESULT GenericStruct([in]LONG a);

Let me keep grinding on this.  I need to eventually get it to work with struct pointers . . . but this is a start !  No points assigned yet !
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.