Hi guys, I wwonder if someone could clear up a few things form in in regards to using non-managed DX and windows forms

To get the most out of Forms, it is advisable not to include windows.h (as it messes up many things not least MessageBox and adding pictures to dialog boxes), _but_ d3d9.h (and others beside) include windows.h themselves meaning that there is no way not to include windows.h.

I have #defined a few of the defines to remove things when you include windows e.g. #define NOGDI, but this just tends to mess DX up, rather than fix anything.

So my main question is, can you actually use Forms and managed C++ with non-managed DX? If you can, how?

Thanks in advance Spree

davepermen
—
2004-08-13T11:09:54Z —
#2

i bet you can.. somehow.. with com interface wrappers..

then again.. if you use winforms, your code will be managed. interfacing a lot between managed and unmanaged code is known to be slow, and expensive. i'd suggest staying as full managed as possible instead..

bladder
—
2004-08-14T02:28:52Z —
#3

Ack. This is going to get very annoying for you really fast

Now if I understand you correctly, you've already programmed a bunch of routines and wrappers in C++ and you want to use them from within the MSVC# IDE and make some editors or something? If that's the case then read on, but if you just want to use raw unmanaged DX from a managed app then just go with managed DX (why wouldn't you?)

Anyway, the way to use unmanaged in managed is this:

You need to create a DLL of your unmanaged classes/functions with the \Clr compiler option. the /Clr option creates managed compatible data (MSIL code). If you're using visual c then this option can be turned on by going to project properties and just choosing "compile using managed extentions". Your code can still be unmanaged however.

So you have to compile everything into a DLL, you're going to need accessor functions for all public member variables as well (that's the worst part of this IMO)

* ALSO, You need to create a C like function that returns a new pointer to the object you are going to be creating.

Now you'd compile that to a DLL using the Clr compiler option, and you can add that dll as an assembly in a managed project.

Next get a program called Dependancy Walker (http://www.dependencywalker.com/). It will give you the name of the functions within a DLL. Since C++ uses name mangling, you need this program to tell you the "name" of the function you want to use in a managed environment. It will be something like:

<?print@MyClass@@PADH@Z>

and get the name for the CreateMyClass function too.

After that you need to create the same class "MyClass" in your managed project and import the functionality from the previously generated .NET compatible DLL using the DllImport attribute:

If you are going to be having memory that needs to be deallocated in the C++ object then you are going to have to override the Dispose method of the managed class and call your C++ destructor from in there. A nice tip is: Keep all you initialization and deinitialization in functions other then the constructor so you have total control over their lifetime...

Also note that all imported function prototypes need to be "static and extern".

You know what, I dont think Windows Forms is worth all that at all! Thats a crazy amount of work for a small payback in my opinion. Shame really cause i was looking forward to creating some spiffy shiney new things, but i think its better for my sanity if i stick with MFC...

Thanks Bladder for making me realise I do not want to do that

Spree

bladder
—
2004-08-14T08:59:25Z —
#6

Yeah, it is quite a bit of work. I was really excited about making some editors with winforms as well. But at the end I stuck with MFC.

Though maybe someone else knows an easier way....

davepermen
—
2004-08-14T11:44:08Z —
#7

visual studio.net EE beta 2005 with C++/CLI does indeed make it easier.. you can freely mix managed and unmanaged.

still.. the toggle is rather slow and not suggested. bether just move over completely.. or not at all..

but believe me.. once you moved over (and short after, moved to c#), you will understand that it's worth it. actually.. then again, i'm only working with .NET 2.0 here..

visual studio.net EE beta 2005 with C++/CLI does indeed make it easier.. you can freely mix managed and unmanaged.

Do you mean unmanaged C++ with managed C++? or unmanaged C++ with C#? If it's the former then the current visual studio lets you do that very easily as well, if it's the latter then .... joy

davepermen
—
2004-08-16T08:01:47Z —
#9

languages don't mather in .NET at all.. well, at least not in 2.0..

you can, indeed, write classes partially in c#, partially in c++, and the c++ part can be managed + unmanaged, and all the fuzz.. strange somehow:D

C++/CLI is NOT managed c++. it's a new language, and makes the managed/unmanaged mixing explicit in code, but implicit in usage (a.k.a. you don't have to think about it anymore really, but you can use it freely). managed c++ is out.

C++/CLI allows both managed and unmanaged code to get generated. but without the pain of managed c++ (both on coding, and on runtime side).

but short afterwards, you'll only continue with c# anyways.. i never thought i'll do.. at least...