0

I understand that the DLL typically has its own heap that it stores variables in. My DLL function takes a pointer to a wchar_t variable, and whenever I try to put a value in it, it simply exits the function, leaving the pointer pointing to a bad location which I'm assuming is because the heap gets destroyed.

If my DLL function comes up with some sort of data that needs to be passed back, could someone give me an example of how I could get that data in string format back to the original main function?

Using Visual Studio 2010.

Edit: I can provide some sample code, but I didn't see the point since I'm simply asking for an example/ explanation as to how memory is handled with regards to dll's and their functions. Ask me what information you need and I'll try to deliver.

Well, to give you guys an idea as to what the application does, it's a COM server DLL. The interface is IProperty, the object is called PropertyObject. The DLL was built separately, by me, with the PropertyObject methods. This method, Getproperty, is the one I'm working on.

STDMETHODIMP PropertyObject::GetProperty(int arg1, wchar_t* arg2)
{
    arg2 = L"Test";
    cout << *arg2 << endl;
    return S_OK;
}

int main()
{

CoInitialize(NULL);

IClassFactory * pClassFactory = NULL;
HRESULT hr;
hr  = CoGetClassObject(
CLSID_PropertyObject,
CLSCTX_SERVER,
NULL,
IID_IClassFactory,
(void **) &pClassFactory);

if (SUCCEEDED(hr))
{
    wchar_t x = NULL;
    IProperty *pProperty = NULL;

    hr = pClassFactory->CreateInstance(NULL, IID_IProperty, (void **) &pProperty);
    hr = pProperty->GetProperty(2, &x);
    cout << x << endl;
}
    return 0;
}
prototypik
  • 2,726
  • 1
  • 18
  • 21
  • 1
    Your existing code would help. Then we can see better what you are trying to do. Most likely you failed to allocate memory before trying to write. – David Heffernan Mar 12 '13 at 16:52
  • Check http://stackoverflow.com/questions/2266218/memory-heap-management-across-dlls – devshorts Mar 12 '13 at 16:52
  • What gives you the idea that each DLL has its own heap? I've made extensive use of DLLs in at least three different environments, and I've never had problems allocating on one DLL, and freeing in another. (I have had problems constructing in one DLL, and doing a `dynamic_cast` in another.) – James Kanze Mar 12 '13 at 17:11
  • Thats not just a DLL "function"; It's a COM interface method. If you're going to use COM and lower-level interfaces (not like IDispatch or automation-compliant dual-interfaces, and if that sounds like a foreign language to you, maybe that should tell you something) I would *strongly* suggest you spend time to learn proper use of IDL (Interface Definition Language). Your code should, for example be using the [`[size_is]`](http://msdn.microsoft.com/en-us/library/windows/desktop/aa367164(v=vs.85).aspx) attribute for conveying buffer space (and in truth, a `BSTR` out-param would be a better fit). – WhozCraig Mar 13 '13 at 02:24

2 Answers2

0

If you are 100% sure about the fact that all participating programs are compiled with the same version of Visual Studio (which implies they all use the same version of the STL that std::string is part of), you can use the std::string class.

If it needs to be interoperable, your best bet is passing in a char* and a length and write to that supplied buffer. Let the caller handle the memory. That's pretty C-style, but also your safest bet.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
0

Turns out I was still considering a wchar_t pointer like a normal character array. here is my revised code:

STDMETHODIMP PropertyObject::GetProperty(int arg1, wchar_t* arg2)
{
    wcscpy(arg2, L"Test"); // This is the function that i needed to be using. 
    return S_OK;
}



int main()
{
    CoInitialize(NULL);

IClassFactory * pClassFactory = NULL;
HRESULT hr;
hr  = CoGetClassObject(
CLSID_PropertyObject,
CLSCTX_SERVER,
NULL,
IID_IClassFactory,
(void **) &pClassFactory);

if (SUCCEEDED(hr))
{
    wchar_t *x = new wchar_t; // Before, this was a normal variable. Changed it to a pointer. 
    IProperty *pProperty = NULL;

    hr = pClassFactory->CreateInstance(NULL, IID_IProperty, (void **) &pProperty);
    hr = pProperty->GetProperty(2, x); // Passed the pointer instead of an address to a normal variable. 
    wcout << x << endl; // wcout instead of cout. It worked. 
}
    return 0;
}
prototypik
  • 2,726
  • 1
  • 18
  • 21
  • "Turns out I was still considering a wchar_t pointer like a normal character array." That wasn't really the problem. The problem is that you were modifying the pointer, the address, rather than populating the buffer. – David Heffernan Mar 12 '13 at 21:32