I have a vendor application that I believe was built using either C or C++, and it has a particular call it can make to a DLL to allow for expansion in functionality. I am trying to work up a POC for that dll, and am working with only this as the specification:
extern int __stdcall LIQEXIT3_LoadCustomer(const char* name, char* id);
I should be able to pass in a char * name and then return that same char * back in the id field. Unfortunately, that isn't what is happening. I either get an access violation, or everything works without error, but the calling application doesn't get the new information within the ID field.
I have created the following DLL and header file code: TestDLL.h
#ifdef TestDLL_EXPORTS
#define TestDLL_API __declspec(dllexport)
#else
#define TestDLL_API __declspec(dllimport)
#endif
namespace TestDLL
{
class TestDLL
{
public:
static int __declspec(dllexport) __stdcall TestDLL::TestMethod_LoadCustomer(const char* name, char* id);
};
}
TestDLL.cpp
// TestDLL.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include "TestDLL.h"
#include <string>
namespace TestDLL{
extern "C" int __declspec(dllexport) __stdcall TestDLL::TestMethod_LoadCustomer(const char* name, char* id)
{
std::string test(name);
if (test.size() <= 8) {
id = (char*)malloc(strlen(name)+1);
strcpy(id, name); // name contains customer id
} else {
id[0] = 0; // Customer not found
}
return 0;
}
}
When I do a simple call to the TestMethod_LoadCustomer method and I have the malloc for the id field in the code, everything seems to progress fine, but my calling code does not get the updated id field. If I remove the malloc call, I get an access violation. I believe the violation is due to the fact that the id field will be placed into read only memory and cannot be directly overwritten.
How do I code this so that the calling application receives the updated values for the string in the id field?
Here is the test app code I put together, which I would expect to take the name "777777" and move it to the id field so that it can be printed to the console: RunTest.cpp
// RunTest.cpp : Defines the entry point for the console application.
//
#ifdef TestDLL_EXPORTS
#define TestDLL_API __declspec(dllexport)
#else
#define TestDLL_API __declspec(dllimport)
#endif
#include "stdafx.h"
#include <iostream>
#include "TestDLL.h"
using namespace std;
int _tmain()
{
char* id= "";
TestDLL::TestDLL::TestMethod_LoadCustomer("777777", id);
cout << id << "\n";
cout <<"This is a Test\n";
cin >> id;
return 0;
}
Thanks for taking the time to take a look at my issue!