W
WebSnozz
I have an application written in C that does a lot of low level stuff.
It does a lot of things like casting from void*'s. I want to create a
new GUI for it in either C# or MC++, but reuse the existing code.
The options I've considered so far:
1. Create a new MC++ GUI project and add the *.c files to them and mark
them with pragma unamanged. However, the /clr option does not compile
*.c files, so I rename them to *.cpp, and now they will not compile due
to c operations not legal in cpp. Most of the compilation errors are
related to perform casts such as:
(classname*)somethingDeclaredAsVoidPointer;//compile error indicates
unable to perform cast from void* to classname*.
I think there is a specific syntax I can use to make these casts
compile successfully(I understand these types of casts are not type
safe), but my goal is to modify the code as little as possible, and
there a very large number of errors in the C code when I try compiling
as cpp. It's not a big task to change them, but I wanted to try and
use the same c files for compiling the old version of the app also,
which would probably involve some #ifdef statements to allow
inclusions/exclusion of code depending on what project it's being
compiled from.
2. Compile the C code as a DLL. Make calls to the DLL from MC++ or C#
whenever a function is needed. I am not quite sure how to do this. I
took the C project, and changed the output type from Application to DLL
and with a few tweaks, got it to compile successfully. However, if I
set the DLL project as a dependency of my C++ project, then it gives me
errors stating the I cannot link with a native DLL because the clr
option is enabled(referring to the clr option in the MC++ project).
So I have no idea how to use the native DLL from MC++. The only
example I found was actually compiling the C code as *.cpp files with
the /clrldsyntax option. This option is not compatible with *.c
files, and changing them to *.cpp files puts me back where I was with
option #1.
This is the part where I lack understanding of native C/C++ DLL's.
I've never made a native DLL before and successfully used it. I have
no problems in .NET, but of course things seem to be alot simpler in
..NET, as public class are automatically exposed, and register the DLL,
and then other applications just referrence the DLL via it's namespace.
How does my MC++ application get knowledge of and make function calls
into functions in the native C DLL?
3. Have the C program continue to run as it's own application, but not
show it's GUI, and instead, post message or communicate with the C# or
MC++ application. I don't know if I can do this with some kind of
posting of messages, or communicating over sockets. I have some
conceptual ideas, but the ideas are way beyond me.
4. Compile the MC++ as a native DLL, and have the C application make
calls into the DLL to spawn the GUI and communicate over function calls
which make requests of the GUI thread. I am pretty confident with
threading in either context. The part that I am wary of, is again how
to make DLL calls. I haven't tried this, but I imagine I will have the
same problem as #2. I don't have a lot of experience with native DLLs,
and if I try to implement this just as it would have been in old school
C/C++, then I'm afraid my efforts will not be fuitful when I found out
that for some reason, once again the /clr option tells me I can't link
to a MC++ DLL from a C application. If there's something special I
need to do, I need to know that.
I think option #2 or #4 are probably my most preferred options, but I
am having trouble figuring out how to give one application "knowledge"
of the other DLL. I'm used to scenarios in C# where I just add a
reference, and walla, I can use the namespace, or in C++ where I just
set a dependency to the DLL and VS seems to magically take care of
linking them together. I am spoiled by VC++. The only time I haven't
used it either when using languages not supported by VS, or when
writing under linux, but have never made a DLL in either of those
contexts.
Any suggestions are appreciated, or pointing me to good examples of
these scenarios that are using truly native C(not cpp files compile
with /clrldsyntax).
It does a lot of things like casting from void*'s. I want to create a
new GUI for it in either C# or MC++, but reuse the existing code.
The options I've considered so far:
1. Create a new MC++ GUI project and add the *.c files to them and mark
them with pragma unamanged. However, the /clr option does not compile
*.c files, so I rename them to *.cpp, and now they will not compile due
to c operations not legal in cpp. Most of the compilation errors are
related to perform casts such as:
(classname*)somethingDeclaredAsVoidPointer;//compile error indicates
unable to perform cast from void* to classname*.
I think there is a specific syntax I can use to make these casts
compile successfully(I understand these types of casts are not type
safe), but my goal is to modify the code as little as possible, and
there a very large number of errors in the C code when I try compiling
as cpp. It's not a big task to change them, but I wanted to try and
use the same c files for compiling the old version of the app also,
which would probably involve some #ifdef statements to allow
inclusions/exclusion of code depending on what project it's being
compiled from.
2. Compile the C code as a DLL. Make calls to the DLL from MC++ or C#
whenever a function is needed. I am not quite sure how to do this. I
took the C project, and changed the output type from Application to DLL
and with a few tweaks, got it to compile successfully. However, if I
set the DLL project as a dependency of my C++ project, then it gives me
errors stating the I cannot link with a native DLL because the clr
option is enabled(referring to the clr option in the MC++ project).
So I have no idea how to use the native DLL from MC++. The only
example I found was actually compiling the C code as *.cpp files with
the /clrldsyntax option. This option is not compatible with *.c
files, and changing them to *.cpp files puts me back where I was with
option #1.
This is the part where I lack understanding of native C/C++ DLL's.
I've never made a native DLL before and successfully used it. I have
no problems in .NET, but of course things seem to be alot simpler in
..NET, as public class are automatically exposed, and register the DLL,
and then other applications just referrence the DLL via it's namespace.
How does my MC++ application get knowledge of and make function calls
into functions in the native C DLL?
3. Have the C program continue to run as it's own application, but not
show it's GUI, and instead, post message or communicate with the C# or
MC++ application. I don't know if I can do this with some kind of
posting of messages, or communicating over sockets. I have some
conceptual ideas, but the ideas are way beyond me.
4. Compile the MC++ as a native DLL, and have the C application make
calls into the DLL to spawn the GUI and communicate over function calls
which make requests of the GUI thread. I am pretty confident with
threading in either context. The part that I am wary of, is again how
to make DLL calls. I haven't tried this, but I imagine I will have the
same problem as #2. I don't have a lot of experience with native DLLs,
and if I try to implement this just as it would have been in old school
C/C++, then I'm afraid my efforts will not be fuitful when I found out
that for some reason, once again the /clr option tells me I can't link
to a MC++ DLL from a C application. If there's something special I
need to do, I need to know that.
I think option #2 or #4 are probably my most preferred options, but I
am having trouble figuring out how to give one application "knowledge"
of the other DLL. I'm used to scenarios in C# where I just add a
reference, and walla, I can use the namespace, or in C++ where I just
set a dependency to the DLL and VS seems to magically take care of
linking them together. I am spoiled by VC++. The only time I haven't
used it either when using languages not supported by VS, or when
writing under linux, but have never made a DLL in either of those
contexts.
Any suggestions are appreciated, or pointing me to good examples of
these scenarios that are using truly native C(not cpp files compile
with /clrldsyntax).