Page 1 of 1

Passing a structure to a DLL

Posted: Fri Mar 18, 2011 6:54 am
by ozzie
I'm wanting to split my monolithic program into two or more compilable units, so I'm planning to keep all the GUI handling in my main program and place the bulk of the processing logic into a DLL. An issue I'm concerned about with this approach is that I have some large structures and arrays that I need to access from both the program and the DLL. Only the DLL will update this information, but the GUI program needs to be able to read it.

So in the GUI program, I may send a request to the DLL to give me the details of a cue, held in a structured variable in the DLL. All the structure definitions are held in .pbi include file which is included in both the GUI program and the DLL, so I can guarantee compatibility.

My question is, how can I pass this structured variable from the DLL to the GUI program? I can see that I could get the DLL to return a memory pointer, but just doing CopyMemory into an equivalent structured variable in the GUI program I don't think will work, especially regarding string variables within the structure. I've searched the Forum and did find some info about the Windows function CreateFileMapping, in the topic Simple shared memory using file maps on Windows. Is this the way to go or is there a better solution?

Re: Passing a structure to a DLL

Posted: Fri Mar 18, 2011 7:10 am
by GBeebe
*MyVar.MyStructure = Your_Function()

Sorry, I don't have PB installed on this computer. You don't have to use copy memory like you said. You can assign a pointer, defined as a structure, a memory value returned by the dll. So your functions can do something like this:

ProcedureReturn @DLLVar.MyStructure

And that should work as long as the memory in the dll is Global.

Re: Passing a structure to a DLL

Posted: Fri Mar 18, 2011 7:19 am
by eesau
The way I usually do it is that I create a bunch of procedures for interfacing with the dll, i.e. single procedures that return a single variable. This keeps the main program and the dll nice and separate, but might be a bit tricky if the data you need to pass is complex.

Re: Passing a structure to a DLL

Posted: Fri Mar 18, 2011 11:02 pm
by ozzie
Thanks for the suggestions. I'll try GBeebe's method. I've got far too many fields within structures to call procedures to get each field separately, but I agree this is a good solution for small numbers of fields.