473,387 Members | 1,453 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Get C string into C#

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!
Nov 16 '05 #1
6 1276
You just need to get the char array and transform it on a string.

Create a little function for this taks.

Tom.

"Aku" wrote:

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!

Nov 16 '05 #2
Aku
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!
Nov 16 '05 #3
Aku
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!
Nov 16 '05 #4
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 Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Aku" <an*******@devdex.com> wrote in message
news:OJ*************@TK2MSFTNGP10.phx.gbl...

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!

Nov 16 '05 #5
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 Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Aku" <an*******@devdex.com> wrote in message
news:OJ*************@TK2MSFTNGP10.phx.gbl...

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!

Nov 16 '05 #6
Aku
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!
Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site......
5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
2
by: Andrew | last post by:
I have written two classes : a String Class based on the book " C++ in 21 days " and a GenericIpClass listed below : file GenericStringClass.h // Generic String class
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
2
by: Badass Scotsman | last post by:
Hello, Using VB and ASP,NET I would like to be able to search a STRING for a smaller STRING within, based on the characters which appear before and after. For example: String1 = " That was...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
11
by: ramu | last post by:
Hi, Suppose I have a string like this: "I have a string \"and a inner string\\\" I want to remove space in this string but not in the inner string" In the above string I have to remove...
8
by: drjay1627 | last post by:
hello, This is my 1st post here! *welcome drjay* Thanks! I look answering questions and getting answers to other! Now that we got that out of the way. I'm trying to read in a string and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.