02-26-2020 01:03 AM
I am trying to call a function in a DLL that I do not control. The interface for the DLL is in ANSI C. One function takes a void pointer. When I looked at some MFC example code provided with the DLL, the struct being passed as the void pointer includes several CStrings. I need to create a compatible struct that I can pass to this function in the DLL. I tried just using void * in place of each CString like this:
struct MyStruct
{
void *Name; // originally CString Name;
}
The call to the DLL function does not work correctly. I don't know if the issue is the struct I am passing or not, but that seems likely.
What type should I use in C in place of CString that I can pass to the DLL function?
02-26-2020 04:46 AM
I'd create a glue dll in c++ which maps the char[] from CVI to the CString instances for the other c++ dll.
02-26-2020 09:23 AM
Unfortunately I haven't used VC++ since 1994 so that would be a bit of a learning curve. And even more unfortunately, the more I think about it, the more I think I can't do what I need to do which is create some memory storage in my CVI program that I can pass to the DLL and have it populate. From what I remember, the storage for an instance of a C++ class includes a pointer to the base class so the compiler can find all the member function pointers. Since the DLL is expecting the CStrings to already be defined, it is also expecting that base class pointer to be in the memory area allocated to those CString instances. Since CVI doesn't know anything about CStrings it can't populate the base class address in memory.
I looked through the variant conversion functions to see if there were any that convert char * to CString and back again but there aren't. And even if they're were, that would be a run time conversion. The way this struct that is being passed to the DLL is setup, I need statically allocated memory for a CString.
03-01-2020 12:21 PM - edited 03-01-2020 01:09 PM
CString is an object. While that is logically a equivalent to a structure there is no standardized way how the internal memory layout of that structure looks like. It can and will vary between C compilers and even compiler versions as it will internally (re)allocate memory and that will depend on the linked in C++ runtime library which varies between VC versions!
So you need to allocate that object with a compatible C++ compiler, and that is something CVI never can be!