How to pass a struct from C++ application to a C++ Win32 DLL?
Would you please give me an example?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can pass a pointer because the DLL in windows are in the same address space as the program.
Note however that the DLL interface stops there and you (normally) do not have a shared memory management and you cannot in general pass for example an
std::vectorand expect the DLL to be able topush_backnew elements. The reason is that the DLL has in general its own memory manager and heap, separated from the one of the caller (after all DLL can be called from any language, not necessarily C++).Even if it may be surprising passing an
std::mapand having the DLL and only just reading it still may not work, because some containers in the standard library depends on “sentinels” and and these could be duplicated for the DLL too.In the above I used the term “in general” because the DLL may have some trickery to be able to share the memory management with the main process. For example microsoft MFC was designed to work properly around these DLL barriers since VC6 (but NOT the standard library!).
You must also be sure that DLL and the main program are compiled with the exact same compiler and compiling options because otherwise even plain structures may have a different memory layout.