Get C string into C#

  • Thread starter Thread starter Aku
  • Start date Start date
A

Aku

Hi,

My C# app is talking to a unmanaged Win32 DLL - the DLL is producing a
string in a char array.
I need to get that string information into my C# code - how to do it?
Thanks!



*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
You just need to get the char array and transform it on a string.

Create a little function for this taks.

Tom.
 
Thomas,
Please elaborate - I don't know how to do it ... .
Here are some of the relevant snippets of my code:

Unmgd DLL code:
SIMPLEDLL_API char* getname(InfStruct* A_info)
char string[50];
..... // data gets into the array
return string;
//----------------------------------------------
SIMPLEDLL_API char* fnstrSimpleDLL(void)
{
char* mystring;
mystring = getname(&InfBuff);
...
return mystring;
}
//-----------------------------------------------

Managed part:
[DllImport("SimpleDLL.dll", EntryPoint="fnstrSimpleDLL")]
public static extern string fnstrSimpleDLL();

//---------------
private void button1_Click(object sender, System.EventArgs e)
{
int retval = fnSimpleDLL();//Invoke the DLL function
...
string x = Convert.ToString(fnstrSimpleDLL());
// this doesnot work ...!
}


*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Aku,

You will most likely want to use one of the static methods on the
Marshal class. Can you give the original declaration of the function in the
C DLL? You might not have to do much more than attribute the method
declaration correctly.
 
Nicholas,
Here is the requested information - hopefully you can help me out --
Thanks!
=================
// SimpleDLL.cpp : Defines the entry point for the DLL application.

int retstatus;
InfStruct myInfo;
char string[50];

/*----------------------------------------------------------------------
-------------*/
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)

{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
// Initialize once for each new process.
// Return FALSE to fail DLL load.
MessageBox(NULL,"SimpleDLL: DLL_PROCESS_ATTACH","SIMPLE DLL",MB_OK);
break;
...

case DLL_PROCESS_DETACH:
MessageBox(NULL,"SimpleDLL: DLL_PROCESS_DETACH","SIMPLE DLL",MB_OK);
break;
}
return TRUE;
}
/*--------------------------------------------------------------------*/
// This is an example of another exported function.
SIMPLEDLL_API char* fnstrSimpleDLL(void)
{
getname(&myInfo);

return string;
}
/*----------------------------------------------------------------------
-----------------*/
SIMPLEDLL_API char* getname(InfStruct* A_info)
{
retstatus = Read_Value_To_String(A_info->recordID, ATTRIB_NAME,
sizeof(string),string);
return string;
}
/*----------------------------------------------------------------------
-----------------*/

************************************************************************
*
//HEADER FILE:
#ifdef SIMPLEDLL_EXPORTS
#define SIMPLEDLL_API __declspec(dllexport)
#else
#define SIMPLEDLL_API __declspec(dllimport)
#endif
...

SIMPLEDLL_API int fnSimpleDLL(void); // Exported function
SIMPLEDLL_API char* fnstrSimpleDLL(void);

SIMPLEDLL_API char* getname(InfStruct* A_info);





*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Back
Top