Hi, I have a HUGE problem with some code I'm writing, and I was wondering if anyone could advise me. BTW, I'm not at all a beginner to programming, but I'm a beginner to the specific APIs I'm using, and using DLLs in general, so please keep that in mind when you reply.
I'm using C# (VS2012 Express) in Windows 7 with XP compatibility, and I'm trying to use the Razer Hydra API (although the problem may not be specifically about this, but DLL usage in general, and I think that very well may be the case).
I've already sent this problem to Razer and after going back and forth with them, answering all their questions, they never actually gave me any suggestions and finally just told me to contact Sixense (the company that made the API) directly, which I did, and I haven't gotten a response for over 3 weeks. So needless to say, I'm quite frustrated and if anyone could help me I would REALLY appreciate it! Thanks.
So here's the problem:
I'm using the DLL, and I tried using it in a C++ project (in .NET), and it works fine, gives me good results, etc. However, it would be much more convenient for me to use it in C#, so as to be compatible with other code I have written. So I put it into C# using the Interop Services thing (I may not remember all the terminology and don't have the code with me at this computer so please humor me).
So anyway, it compiles, runs, and calls the functions correctly without causing exceptions. However, I don't get the data back that I expect to get returned. I'm using the functions in the same way and order that I use them in my C++ code, but in C# I get different results for some return values.
I especially get different results in a struct parameter that is byref. It's a variable that holds all the data for the current state of the currently selected Hydra controller (stick positions, position of the controller itself, buttons pressed etc.). There's a function that I call to retrieve all the data at once into this structure, and it gives me weird results:
- The button states are stored in a short, but it's always a random number each time I get it, and doesn't correctly represent the actual state.
- There's a 4x4 matrix of floats that holds vectors for the direction/orientation of the controller, and it returns the value "1" in the x, y and z coordinates of the first column, but all other values are "0". This may be a default matrix, but suggests that it might be communicating correctly to some extent (instead of all "0" or random data), except not getting the correct data for some reason.
- All other values in the struct (analog stick and trigger position, etc.) are always "0".
Notice, the way I built the struct was different in C# than the way the DLL specifies it in C++. The DLL uses arrays of fixed size. All arrays in C# are referential, so they wouldn't exist inline, so instead I just put float variables like: mat00, mat01, mat02 ... and so on. I think I sometimes had to change from one data type to another of the same size. There may have been a couple other little things I changed to try to make it compatible, but I think I used a class rather than a struct (can I even use a struct in C#? I never tried.)
So ultimately, I just need it to give me the right results. I'm not sure if I set the parameter up correctly. Does anyone know the correct way to make a C# object compatible with a C++ struct such that a DLL function would recognize it correctly? Or am I using the wrong calling convention or something? I remember setting it up to something like cdecl or call-something or other (I really apologize that I don't have the info with me).
Or is it just a Hydra-specific problem and if so, does anyone know how to fix it?
P.S.: I also want to do essentially the same thing with the Novint Falcon API/DLL. But that one requires I set up a couple callback functions using function pointers. Is there anything I need to know about operating with callback functions between a C# and a DLL written in C++?
Thank you very much!