Managed C++ calling unmanaged C++ dll

G

Guest

I have a managed C++ dll,, which is called from C#. The managed C++ module
then calls a non-managed C++ dll which returns a linked list. If I pass in a
beginning node, and then have the non-managed C++ dll add links to the
existing node, everything works fine. If I pass in a pointer-to-a-pointer to
a linked list element, after returning from the function, the pointer has
undefined values for the two elements.

If I change the calling dll to unmanaged, it works fine.

Here is the code for the two sample modules:

The Managed dll code:

// LinkedListApp.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "IPrintMigrate.h" // the unmanaged dll header

int _tmain(int argc, _TCHAR* argv[])
{
LinkedList __nogc *linkedList = NULL;
LinkedList __nogc *ptr = NULL;
if (GetPSMPrinters("abcde", &linkedList))
{
ptr = linkedList;
while (ptr)
{
//paList->Add(new String(ptr->name));
printf("printer %s\n", ptr->name);
ptr = ptr->next;
}
}
return 0;
}


The Unmanged dll code:

// IPrintMigrate.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "IPrintMigrate.h"
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}

// This is an example of an exported variable
IPRINTMIGRATE_API int nIPrintMigrate=0;

// This is an example of an exported function.
IPRINTMIGRATE_API bool GetPSMPrinters(char *charIPAddress, LinkedList
**linkedList)
{
*linkedList = new LinkedList();
strcpy((*linkedList)->name, "first one");
LinkedList *a = new LinkedList();
strcpy(a->name, "second one");
a->next = NULL;
(*linkedList)->next = a;
return true;
}

What do I need to do so that I don't need to allocate the first node in the
calling routine?

- Bruce
 
G

Gary Chang[MSFT]

Hi Bruce,
If I change the calling dll to unmanaged, it works fine.

It appears the pointer-to-a-pointer could not be passed correctly while
crossing the managed/unmanaged boundry, according to your code snippet,
there is no managed object in it, do you have trie to wrap these code in a
unmanaged complie block:
#pragma unmanaged
...
#pragma managed


Thanks!

Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
 
G

Guest

I will try this and see if it works.
- Bruce

Gary Chang said:
Hi Bruce,


It appears the pointer-to-a-pointer could not be passed correctly while
crossing the managed/unmanaged boundry, according to your code snippet,
there is no managed object in it, do you have trie to wrap these code in a
unmanaged complie block:
#pragma unmanaged
...
#pragma managed


Thanks!

Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Gary (or whatever your name really is):
I changed the code in the calling routine to the following but got the same
result:

#pragma unmanaged
bool MyGetPSMPrinters(char *name, LinkedList **pPtr)
{
return GetPSMPrinters(name, pPtr);
}

#pragma managed
int _tmain(int argc, _TCHAR* argv[])
{
LinkedList __nogc *linkedList = NULL; // new LinkedList();
LinkedList __nogc *ptr = NULL;
if (MyGetPSMPrinters("abcde", &linkedList))
{
ptr = linkedList;
while (ptr)
{
//paList->Add(new String(ptr->name));
printf("printer %s\n", ptr->data);
ptr = ptr->next;
}
}
DeleteList(linkedList);
delete linkedList;
return 0;

}
 
G

Guest

I changed the code as follows and now it works, so your advice did work.

// LinkedListApp.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "IPrintMigrate.h"

#pragma unmanaged
void MyGetPSMPrinters()
{
LinkedList __nogc *linkedList = NULL; // new LinkedList();
LinkedList __nogc *ptr = NULL;
if (GetPSMPrinters("abcde", &linkedList))
{
ptr = linkedList;
while (ptr)
{
//paList->Add(new String(ptr->name));
printf("printer %s\n", ptr->data);
ptr = ptr->next;
}
}
//DeleteList(linkedList);
return;
}

#pragma managed
int _tmain(int argc, _TCHAR* argv[])
{
MyGetPSMPrinters();
return 0;
}
 
G

Gary Chang[MSFT]

Bingo! Bruce,

I am glad to know you resolve the problem, good luck!


Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top