Calling managed code form unmanaged code

M

Martin Groh

Hey, I making some C code that is suppose to run on a microcontroller, but I
would like to test some modules in the program on my computer (using VS pro
2008).
Now I have made the module in plain C (and it compiles nicely), now I have
done this under a CLR Windows Form Application project.
Now my module is suppose to read/write for serial port, and I have made a
"fake" driver for this.
I am using a normal windows form to control the input to this driver class
(textboxes, buttons etc.)
So all the communication from the Form to the driver class works perfectly,
but my PROBLEM is that I can't find a way for the drive class to call
function in the Form class. I always and up whit "global or static variable
may not have managed type" when I try to make references or something.

To summarize I need a way to make my drive class send a String to a rich
textbox on the form.

Regards Martin
 
M

Mark Salsbery [MVP]

I don't think you can do it from C, but I'm often wrong.

You could use C++/CLI however, perhaps in a DLL, to bridge to the managed
code.

Mark
 
P

Pavel A.

Martin said:
Hey, I making some C code that is suppose to run on a microcontroller,
but I would like to test some modules in the program on my computer
(using VS pro 2008).
Now I have made the module in plain C (and it compiles nicely), now I
have done this under a CLR Windows Form Application project.
Now my module is suppose to read/write for serial port, and I have made
a "fake" driver for this.
I am using a normal windows form to control the input to this driver
class (textboxes, buttons etc.)
So all the communication from the Form to the driver class works
perfectly, but my PROBLEM is that I can't find a way for the drive class
to call function in the Form class. I always and up whit "global or
static variable may not have managed type" when I try to make references
or something.

To summarize I need a way to make my drive class send a String to a rich
textbox on the form.

Regards Martin

This can be done as follows:

// Callback exported by managed side to plain C:
extern "C"
void GlueFunc( char const *psz )
{
Form1^ f1 = MyForm1::GetInstance();
System::String^ s = gcnew System::String(psz);
f1->someMethod( s );
}


And the MyForm1 class has:

private: static MyForm1^ m_theform;
public: static MyForm1^ GetInstance(void) { return m_theform; }


// The main app:
int main( array<System::String ^> ^args )
{
// Create the main form and run it
MyForm1 ^f1 = gcnew MyForm1();

init_plain_c_code();

Application::Run(f1);

return 0;
}

Regards,
--PA
 
P

Pavel A.

Pavel A. said:
This can be done as follows:

// Callback exported by managed side to plain C:
extern "C"
void GlueFunc( char const *psz )
{
Form1^ f1 = MyForm1::GetInstance();
System::String^ s = gcnew System::String(psz);
f1->someMethod( s );
}


And the MyForm1 class has:

private: static MyForm1^ m_theform;
public: static MyForm1^ GetInstance(void) { return m_theform; }


// The main app:
int main( array<System::String ^> ^args )
{
// Create the main form and run it
MyForm1 ^f1 = gcnew MyForm1();

init_plain_c_code();

Application::Run(f1);

return 0;
}

.... and the form constructor should set m_theform top"this" after the
instance is initialized.
In short - it's kind of singleton :)

Regards.
--PA
 
M

Martin Groh

Thanks a lot, this was just what I was trying on. :)
but as I'm not a C or a C++ programmer I got entangled in some pointer
issues, and made a mess of the whole thing :-s
 
P

Pavel A.

Martin, you're welcome :)

Now what I'd like to ask, how you're going to run
the serial "driver" and other "embedded" code -
this should be in one or more threads?
This is what I haven't done yet.

Regards,
P.
 
M

Mark Salsbery [MVP]

So....just to clarify...

I based my initial response on the OP never mentioning C++, only "plain C".

Does "plain C" compile with the /clr option or is this C++?

If C code can be compiled managed, then I had no idea, nor would I have ever
found out.

:)

Cheers,
Mark
 
B

Ben Voigt [C++ MVP]

Mark said:
So....just to clarify...

I based my initial response on the OP never mentioning C++, only
"plain C".
Does "plain C" compile with the /clr option or is this C++?


Nearly all well-written, portable C code will be accepted by the C++
compiler. While C++ is not a strict superset of C, following C++ rules for
prototyping, etc are good practice in C as well.
 

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