-1

I've created a CLR project and now I need to convert a C# textBox.Text property into a C-ansi characters array (null-terminated). I need to pass the text to a C function, something like this:

UPDATE2:

// Form1.h (C#)
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {

myCFunction(textBoxName.Text); // wrong

}

// utils.h (C) - inside the same project as Form1.h
void myCFunction(char* szName);

// utils.cpp (C) - inside the same project as Form1.h
void myCFunction(char* szName)
{
  // do something
}
13
  • 1
    stackoverflow.com/questions/2793548/… Commented Jun 9, 2013 at 7:44
  • @xaxxon: I believe that's a different situation, as that's converting it for external use - this is for a native function call. Commented Jun 9, 2013 at 7:49
  • The data is the same regardless. Commented Jun 9, 2013 at 7:54
  • 1
    stackoverflow.com/questions/12618747/… Commented Jun 9, 2013 at 8:12
  • 1
    "Form1.h" won't be C# code. It may be C++/CLI, but it's not C# code. It's really not clear what you're doing at the moment, I'm afraid. Commented Jun 9, 2013 at 8:48

2 Answers 2

1

If you're passing it into a C function using P/Invoke, I believe the marshaller will just do it for you, based on attributes applied to the parameter in the declaration.

So based on this documentation, you probably want something like:

[DllImport("YourLibrary.Dll")]
public extern void Foo([UnmanagedType.LPStr] string text)
Sign up to request clarification or add additional context in comments.

4 Comments

Jon, I'm not using an external DLL. Will I need to do it?
@BoleGrat: Well where is the C function that you're trying to call. You'll need some sort of extern call, even if it's a mixed mode assembly.
@BoleGrat: But you're calling the function from C#, right? Which means you should have an extern declaration in the C# code. That's the declaration that I'm talking about in the answer. Have you actually managed to call a C function at all from C# yet? (If this is your first time calling a C function from C#, you've probably got more reading to do first.)
@BoleGrat: Right. So I suggest you start with something you can be more confident in - start with just a function with no parameters at all, and work out how to call that. Then maybe move on to a function with a 32-bit integer parameter - that should be easy to get right - and then move on to strings, using this argument to help you. For example, it's still not even clear where this C function exists. (Is it really in the same DLL as your C# code? I would personally try to avoid that if possible.)
0
char* str2 = (char*)(void*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(textBox1->Text);

Simple.

1 Comment

You should probably mention, that the memory allocated by StringToHGlobalAnsi needs to be manually freed with FreeHGlobal. Also important to know is that you can't specify the used encoding. With StringToCoTaskMemUTF8 (free with FreeCoTaskMem) you know that you get proper utf8.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.