Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!

1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Member Since 26 Jan 2007
Offline Last Active Mar 11 2013 03:54 AM

Topics I've Started

Pointers invalid between C++ and C#.

26 January 2013 - 10:56 AM

I'm experience a bit of a small problem. I have implemented a render system with a dll in C++, where the user injects data (model/view/projection matricse, colors, meshes and so on) through void pointers. It is quite handy, as the graphic API's usually don't care about the data type, just the structure and size of the data.


I have been using it as a part of a game that we are developing, and it works out quite good. But now I am in the mood of developing a editor to the render system in C#, and am quite stumped.


When i link an XNA matrix as a pointer to the render dll, the pointer stays intact in teh transfer to the dll, where it is kept for future references when updated in the editor. But, the data that the pointer points at, is not. Here is some code:


Editor (C#):

                Matrix unsafeWorldMatrix = worldMatrix;
                void* worldMatrixPointer = (void*)(&unsafeWorldMatrix.M11);

                mRenderInterface.AddData("world", worldMatrixPointer);

While the pointer is in the editor code, the world matrix pointer is able to convert back to the matrix in question, with the data intact.

               // The data is intact
                Matrix worldMatrix = *(Matrix*)worldMatrixPointer;


But as soon as I send it to the render system, using the dll, it looses the data. The pointer is the same, but teh first value just points to 0, and the rest is invalid.

       // Sending the data to the render system
       // The data is still intact here, and lets say that the pointer value is 0x0c52af32
        [DllImport(DllName, SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
        static extern unsafe void SetSharedData(System.String dataID, void* data);
        public unsafe void AddData(String id, void* dataPtr)
                SetSharedData(id, dataPtr);

In the dll (c++)

        // Here the data is incorrect
        // the pointer is the same as before, but with CAPS letter (should not matter though)(0x0C52AF32)
        void hs::renderer::SetSharedData(const char* id, void* data)
	        AssertFunctionAndThrow(DirectXManager::GetInstance()->GetContent()->AddSharedData(id, data));

Both systems are compiled using x86 / Win32 settings, and both are in debug.



What is the deal here? I am aware that it is lack of knowledge, as I´m very unfamiliar with the correct procedures here, but as far as i know, the data should not change, as long as the pointer is intact...

BTW, when returning back from saving the pointer in the system, returning to the C# editor, the data in the pointer is intact.... WTF?


So, anyone who can point out my error? And as the rendersystem works as intended in the actual game, I know it is nothing wrong in the dll. And, teh only thing I do is storing the pointer in a map with a string identifier, so no interaction with hte actual pointer,


Any ideas are welcome.


HowTo: Send HWND from managed C# to unmanaged C++

13 January 2013 - 07:45 AM

I'm currently implementing a render system that are scriptable by XML. While the system is kinda awesome, so far, there is a bit of a steep learning curve for others to use the scripts. So I'm in the process to also implement a editor to the render system. The render system is implemented in C++, the editor will be implemented in Windows Forms (C#).


But im already stumped. I try to call the functions from the dll, but i get a

PInvokeStackImbalance was detected.

error. While reading on about the error, I realize that there is a problem with calling conventions and parameters, or, like the error tells me:

This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

But, there should match, or am I at fault here?


The call is the following:

        [DllImport(DllName, SetLastError = true)]
        static unsafe extern void Initalize(IntPtr hWnd);
        public unsafe void Start(IntPtr hWnd)

The dll entry is as follow:

	namespace hs
		namespace renderer

			__declspec(dllexport) void Initalize(HWND hWnd);


I'm, as I said, stumped. First i thought it was the namespaces, but as it finds the entry point, i don't think thats the problem.


Anyone out there who knows the correct way to do this?

Much appreciated.

Any good books?

17 November 2010 - 07:55 AM

Hi all game developing people out there. (Yeah, graphic artists included).

Christmas is soon knocking on the door, and as always I got friends and family asking the annoying question "And what do you want for Christmas?". Anyone who doesn't know what I'm talking about? Good, then you get my dilemma :).

Well, usually I wish for money, underwear, sex toys... ... ... you get the point.

This year, i thought to myself, I need to get something useful that they actually could be able to get me (I don't understand whats the problem with the underwear's). So, this year I'm going to wish for game developing books. Yeah, you heard me. The internet is not big enough to fill my needs, and I have only been jumping between different books that the school have recommended us to get to be able to pass the exams and do the petrifying task to get a triangle to rotate on the screen.

I know that there is a section for books of this kind, but some are even dated back to 2000. It's hard to get a good grip of what is good for me or not, and here i will hopefully get a more fresh update, and with reports from real people with real experience ;).

So now I ask the more experienced audience for some good (preferably great) books that I could be wishing to fill my stockings hanging over the oven (I don't have a fireplace). I have left the beginners crowd some years ago, and I don' really have a specific topic in mind.

Languages: C++ / C# (maybe a new script language book)
Graphical API: DirectX

Just not OpenGl, and that is not that I don't like it, or have wet dreams of Bill Gates, its just that I have been directing my learning curve towards DirectX. And as I have not mastered it to the bones (yet), I would like to dig deeper into the DirectX core rather than start from scratch.

It doesn't have to be language or API specific either, just as long as you think it would be a great additional contribution to my dust collecting areas as I travel through the upcoming years of game development.

Precompiled thanks, and a upcoming Merry Christmas and a happy new year.

PS. Feel free to follow this thread even if you don't contribute, as it's probably more people out there with the same problem. This is a big area and there is hard to keep track of good and bad literature out there. PS.

[Edited by - CAPE on November 17, 2010 6:03:44 PM]

Redist problem, I think.

30 October 2008 - 10:52 AM

I have a mayor problem. I have programmed a car game with physics and 3D rendering. The rendering part is done with Ogre, but I don't think this is of importance. The game have been coded with VS 2008 in c++ , in the OS Vista, and it looks pretty good :). The code compiles and runs without problems on my computer, and on the computers of most of the other guys on my team. But on some computers, all of them without the development base of VS 2008, and all of them with XP, there is a strange crash. First, it complains that the redist files for VS 2008 is missing. After the installation of these, then the real problem starts to show. It says this: "The procedure entry point _except_handler4_common could not me located in the dynamic link library msvcrt.dll" I think it has something to do with the redist files not beeing located on the right place or that the files are incorrect, like if there already is dll files that is out of date and overides the correct files from the redist installation. But I have no clue how to fix this. If you have encounterd this or a similar problem, please tell me if you know the solution, or know what the problem might be.

binding c++ class in LUA

10 September 2008 - 01:00 AM

Hi all fellow programmers. I have a little problem here, and I don't know what it can be. A little backstory: I began writing a AI game including robots with different AI's declared in different LUA scripts. This game and the AI aspect worked without problem. So, I turned in the game to the teacher to grade it, and because of different dependicies and varius VS versions, he could not build the program. So, two weeks later or so, he asked me for a own build. Me, who deleted the compiled executables since before, began to build again, and now it dosn't work. It crahses here:
void RobotManager::BindStateMachine()
			.def("ChangeState", &StateMachine<Robot>::ChangeState)
			.def("CurrentState", &StateMachine<Robot>::CurrentState)
			.def("SetCurrentState", &StateMachine<Robot>::SetCurrentState)

Okay, now I began to wonder, "WF...". The class is correct, the function names are correct, and I have not edited the code, so it should work. Well, the next idé i had was that the linker had been damaged, so the thing I did to check this, i followed a tutorial, and just copied the code for the "Exporting Classes to Lua" example. I linked the files correct on my computer, and it compiled without problem. Guess what... it chrashes on the same operation:
		.def("Print", &NumberPrinter::print)

The declaration of the class works fine, but when it is about to declare the functions inside of it, it break's. And I have not changed the code what so ever after the time it actually did work, and of my knowledge, the dependicies have not changed either (though this seems to be hte problem in this case. If you can see what the problem is in the code (see tutorial under "Exporting Classes to Lua"), that is, if there is a problem, please tell me. Else I don't know what to do. YT CAPE