tags:

views:

98

answers:

2

Hello friends, I'm using an API (written in c++) to connect to a DVR machine, actually I only have the .dll and the .lib files, and I want to do the job in .NET (C#). So, the API doc contains definitions to all functions inside the dll, and I'm having a hard time trying to figure out how to translate those functions to equivalente C# definitions. Some of them are straightforward, but some I cannot make it work.

For example, there is this function:

bool searchEvent(int channel, const LONG* condition, bool next)  

In my c# class I put the following:

[DllImport("search.dll")]  
protected static extern bool searchEvent(int channel,  
[MarshalAs(UnmanagedType.I8)]long condition, bool next);

but when I call the searchEvent function, it raises an error (memory error), I guess because of that long var. So my question is how can I translate "const LONG* condition" to c#? Also, that searchEvent function triggers a callback that returns a struct. I am unable to translate some vars of that struct, like:

char _version[2]  
time_t _time  
BYTE* _minute  
unsigned short int _dwell[MAX]  

Can someone help me with this, please? I´m no expert in c++, so any tips are greatly appreciated. Thanks!

+1  A: 
    [DllImport("search.dll")]
    [return: MarshalAs(UnmanagedType.U1)]
    private static extern bool searchEvent(
        int channel,
        ref int condition,
        [MarshalAs(UnmanagedType.U1)] bool next
    );

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    private struct Mumble {
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)]
        public string version;
        public long time;
        public IntPtr minute;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX)]
        public ushort _dwell[];
    }

You will have to marshal the Mumble.Minute value yourself with Marshal.ReadXxxx(). These declarations are not great for interop, writing a wrapper in the C++/CLI language is advisable.

Hans Passant
thanks for the "Mumble" stuff, very useful to recreate structs... The searchEvent didn't work though.... I'll keep looking, thanks!
The condition argument could be an array too, int[] (no ref). This is just guessing without knowing what they do.
Hans Passant
A: 

Without getting into the specifics, you need to overcome two hurdles: 1) Since you don't have the original .h (header) file, you're guessing as to what it could be, based on the API documentation. Hopefully you'll get it right, but there may be some subtle things in the header file that won't be in the documentation, such as structure packing, calling convention, name mangling, etc. You can start by assuming the defaults for this stuff. Bottom line you have to experiment.

2) You are attempting to create P/Invoke calls into a C DLL when you don't have the original header file. You don't know even know if you know how to make these calls from C, much less C#.

By doing this straight from C#, you're trying to attack these two issues at the same time. when something doesn't work, you're not sure if it's because you go the P/Invoke syntax wrong, or it's an issue related to your interpretation of what the header file content looks like. I'd try to attack these problems one at a time. First, try to make the DLL calls from a simple C console app. This shouldn't be difficult to create, then hand build a .H file, and very that you can make all the calls.

After you're done with that, you now have to get those calls to work from C#.

Frankly, I avoid P/Invoke unless I can just reuse one that someone else created (from http://PInvoke.net) for the Windows API. If I were you, and I already new how to make the calls from C, I'd just create a C++/CLI wrapper DLL for it. The advantage is that, you make the calls exactly as you do in the console app -- no P/Invoke necessary. And then expose those calls from the C++/CLI DLL as .NET. Your C# code will call the C++/CLI DLL, which looks just like (and is) a .NET component, with no weird P/Invoke stuff in the C# code.

Good luck.

zumalifeguard
Hey there, thanks for your insight on this matter... You said it is better to create a c++/cli wrapper for the dll, however I have no idea of how to do that... Is there any "fast" way to do that, or too much code is required? Searching hi and lo I managed to get my hands on the header file (about 500 lines of code), where do I get started?
Here's what I would do. First, without .NET, create an unmanaged C++ DLL that makes the calls you want to the library in question, then create an unmanaged C++ console app to call your C++ DLL, just to make sure everything works. Next, add the /CLR switch to the C++ DLL, fix any errors, build and run from the console app to make sure it still works. Now, you've verified that C++/CLI app can call the unmanaged library. Now, in the DLL, add a managed class and have the managed class make the same calls to the unmanaged library. Test it by calling the managed classes from a C# console app.
zumalifeguard