First off, I'm a C# programmer, so my working knowledge of C++ is fairly limited. I took it back in college, but haven't touched it in 10 years, so please forgive me if this is relatively simple stuff.
I'm attempting to make a DLL that I can use in C# that implements the libwpd library.
I've managed to create a DLL that exports 2 functions that I can access via P/Invoke. The first returns a constant integer (generated by visual studio as a sample), the 2nd a string.
If I return a constant string from the function, it passes successfully to C# and I can read it on the other end, so I know the data is being passed back.
The problem I'm running into is with libwpd. I've had to modify their TextDocumentGenerator.cpp file to add the information to a char* instead of using the printf that they use so I can access it later.
I've added a variable definition to the public section of the header file so I can read it from the calling code.
Now, I'm trying to write a function that allows me to add the char* given by libwpd to the external char*.
I've come up with this:
char* addString(const char* addThis, char* toThis)
{
char* copier = (char*)malloc(strlen(toThis) + 1 + 1);
strcpy(copier, toThis);
strcpy(copier, "1");
toThis = (char*)malloc(strlen(copier) + 1);
strcpy(toThis, copier);
return copier;
}
But when I pass the information back, I get a blank string.
I call the function by calling totalFile = addString("\n", totalFile);
(I realize it should only technically add "1" to the string repeatedly, but it's not doing even that)
If i change the strcpy to strcat for the copier lines, it locks up.
I don't know how to create a program in C++ so I can even step through the functions to see what's happening.
Any assistance would be appreciated.
Are you aware of existence of std::string
? It's a class that handles strings in C++; char *
is legacy from C.
std::string
provides +
operator, that does what you want.