please help with pointers for a dll plugin?

Hi -
I am a novice programmer although I have successfully implemented a few dll plugins with Unity. However, this one is revealing some issues I don’t know how to deal with - which I think is specific to C++ conventions vs C#.

I am trying to use a dll for the 5DT data glove. “fglove.dll” can be downloaded from the 5DT website and I have successfully used in some other c++ applications. The function reference is available as a PDF (http://www.5dt.com/downloads/dataglove/old/5DTDataGloveDriverManual.pdf) Examples, including the first one that opens the glove device in which I can’t figure out, are like this:

fdGlove *fdOpen(char *pPort) 
Initializes the glove device on the specified port. 
 
Return value 
Returns a pointer to the glove device (fdGlove *). NULL is returned if an error occurred.  
 
Remarks 
Do not attempt to alter the contents of the returned pointer directly, use the functions provided 
instead.

So it returns data in the pointer, and this is what I don’t know how to deal with in the C# script in Unity.

public class fGloveUnity : MonoBehaviour {
	
	[DllImport ("fglove")]
	private static extern fdGlove *fdOpen(char pPort) ;

That of course results in an error, “the type or namespace ‘fdGlove’ cannot be found”.
So I was guessing I need to create a structure for it, based on looking at other examples including the midiplugin example on the unity site.

The fglove.h file included with the dll shows this:

typedef struct
{
	// The contents of this struct are platform-dependent and subject to
	// change. You should not manipulate the contents of this struct directly.
	void           *m_pStuff;
} fdGlove;

So if I try something like:

public struct fdGlove
{
void *m_pStuff;
}

I get the error “Pointers and fixed size buffers may only be used in an unsafe context”.

So that is where I am stuck. I’ve seen other threads that talk about setting the unity preferences to allow unsafe, and also Marshaling which I know nothing about, and don’t know that either of those are a step towards solving how the data returned from the device can be accessed.

If anyone can start me in the right direction for this I would appreciate it very much.
thanks

Hi priceap,

Try this:

[DllImport ("fglove")]
private static extern IntPtr fdOpen( [MarshalAs(UnmanagedType.LPStr)] string pPort );

fdOpen returns a pointer to a data structure that you probably do not need to manually access from managed code, therefore just use System.IntPtr.
Also note that “char*” converts to a string.

Do check out this page for more information:
http://www.mono-project.com/Interop_with_Native_Libraries

thanks for the lead Tomas - I will spend some time with it and see if I can get it fully working. I had looked at the interop page, but your example put the right twist on it so the info on the page makes a little more sense for me -
thanks

hallo priceap, did u get the dll work?
currently i’m involve in a project that require to use 5dt data glove and interact with unity 3d environment, and i’m stuck now.
if you successful get it done, did you mind share it ?
very appreciate with that.

Please download the newest C# drivers (v2.4) from http://www.5dt.com/downloads.html . It now supports C# .NET2 and higher, and also includes a MonoDevelop example.

Best Regards,
Dewald
5DT

Im having the same problem… with Visual Studio 2010 and Unity 3.4

using UnityEngine;
using System.Collections;
using FDTGloveUltraCSharpWrapper;

public class guante : MonoBehaviour {

    CfdGlove guanteObject;
    // Use this for initialization
    void Start () {
       guanteObject = new CfdGlove();
       guanteObject.Open("USB0");
    }

    // Update is called once per frame
    void Update () {
       if(guanteObject.IsOpen())
       {
         light.intensity=0;
       }
    }
}

I have the latest drivers for c#, from the 5dt site, and in my assets folder I have the FDTGloveUltraCSharpWrapper.dll and fglove.dll and the scripts… I have the c# file attached to a light… I get the error:

EntryPointNotFoundException:fdOpen
FDTGloveUltraCSharpWrapper.CfdGlove.Open (System.String sPortName)
guante.Start() (at assets/guante.cs:11)

I guess the problem is in the fglove.dll file, how do we know the archive is linked to the wrapper?.. Im novice with both Unity and the glove… please help

If I remember correctly, the C# class in unity does not allow you to link in another dll, so I solved it by writing a C dll with wraps 5dt functionality.

There is no problem with such a linking.
The C# class just has to be in the plugins folder and you need to be on Unity Pro as native code Plugins are a pro only feature.
And naturally the function needs to be present in the dll externed as cdecl externed function, in this case likely as a cdecl externed function with header

fdOpen( char* sPortName)

If the function is not exported as cdecl and thus mangled, definiting the function name requires that you look into the linker map file to find the explicit wrangled name

Hi dreamora!
Well, I’m right now trying to understand how Unity works with dlls
Finally I realized that It wasn’t working cuz I had to copy the fglove.dll file into debug folder, I thougt Visual do this for me, but Visual just copied the “verylargenameofwrapper”.dll into the debug folder… in the moment I copied the other file, the program worked.
Now, Im trying to do the same in my Unity project. Im working with UnityPro, so I can manage plugins, the problem is that I don’t know the folder I have to copy the dll file, cuz when I added the reference from the CSharp file, automatically Unity copied the wrapper (dll) into the assets folder, And didnt created the “plugins” folder. so, intuitively, I tried copying the other dll file in the assets folder too, but it doesnt work still as the VisualStudio project did it.
Could you tell me what to do? I’ll apreciate a lot

nevermind… tried to create the “Assets/Plugins” folder by myself, and it seems to work. Thank you a lot anyway.