PLS HELP: How give string from C# to C++ char *!!!!!!!!!!

Hi all,

also,my problem is:

I have a DLL in C++
and I want link a function of this DLL in the programm language C#:

my problem is passing this string "COM1" from C# to the
type char *port .


---------------------------------------------------------------------
---C++ DLL---
extern "C" _declspec(dllexport) double getT_AV (long baud, char *port)
{
//port should point at "COM1" :char *port = "COM1"
...
}
---
---My C# Code---this code doesn't working
[DllImport("ChamberDriver.dll")]
public static extern double getT_AV(long baud,string port);

private void get_Temp_Click(object sender, System.EventArgs e)
{

long baud = 19200;
string port ="COM1"

double temp = getT_AV (baud,port);

Output.Text = temp.ToString();

}
---
----------------------------------------------------------------------
I hope my description is understandable ;)

thank you for Help !!!

Comments

  • Your problem is not with the string, but since you asked, here's the rule:
    - If the DLL is going to change the content of the string (const char*), pass an System.Text.StringBuilder object
    - If the DLL is not changing the content, (char*), pass a string.

    Now, your real problem is with the "long", C/C++ long is 32-bit, .NET long is 64. So change C# decleration getT_AV(int baud, string ..etc) and you should be fine


    : Hi all,
    :
    : also,my problem is:
    :
    : I have a DLL in C++
    : and I want link a function of this DLL in the programm language C#:
    :
    : my problem is passing this string "COM1" from C# to the
    : type char *port .
    :
    :
    : ---------------------------------------------------------------------
    : ---C++ DLL---
    : extern "C" _declspec(dllexport) double getT_AV (long baud, char *port)
    : {
    : //port should point at "COM1" :char *port = "COM1"
    : ...
    : }
    : ---
    : ---My C# Code---this code doesn't working
    : [DllImport("ChamberDriver.dll")]
    : public static extern double getT_AV(long baud,string port);
    :
    : private void get_Temp_Click(object sender, System.EventArgs e)
    : {
    :
    : long baud = 19200;
    : string port ="COM1"
    :
    : double temp = getT_AV (baud,port);
    :
    : Output.Text = temp.ToString();
    :
    : }
    : ---
    : ----------------------------------------------------------------------
    : I hope my description is understandable ;)
    :
    : thank you for Help !!!
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion