views:

73

answers:

2

Hi,

i need help about custom marshaling..i have a native DLL..the parameters of the some DLL functions is defined with unsigned char*

for example;

typedef short apiStatus;
    apiStatus __declspec(dllexport) __stdcall DrfGetFirmwareVersion (HANDLE hCom, unsigned char *major,unsigned char *minor, unsigned char ReaderAddr = 0xff); 

the documentation that i have describing the arguments of the function as follows;

Input parameter:

  • hCom-Serial port handle
  • major-main version No. of firmware program
  • minor-subversion No. of firmware program
  • ReaderAddr- reader address, for fixed reader RS485 net application, default value “0XFF”(not applicable for handheld and module)

how can i declare this function into C#..??

Any help at all would be really appreciated

+1  A: 

If this DLL is really, really old (back when char was still unsigned) then it could be returning strings. That's pretty unlikely though, just declare them as out byte:

[DllImport("blah.dll")]
private static extern noidea DrfGetFirmwareVersion(IntPtr handle, 
   out byte major, out byte minor, byte address);

Where "noidea" presumably is void or some kind of int error code. Another thing to fret about is the DllImport.CallingConvention property, it could be Cdecl. Use that when you get a MDA warning about a stack imbalance. And don't forget to set your project's Platform Target to x86 or it will bomb on 64-bit operating systems.

Hans Passant
thanks for the feedback..i have set the platform target to x86 like you said..another problem is the documentation explains a little things for the functions..sometimes i couldn't determine what i should do..the SDK of the device containsa lib file..i opened the lib with an editör and i encountered decorated names.i undecorated these decorated names with undname.exe..the results as follows; undname ?DrfGetFirmwareVersion@@YGFPAXPAE1E@Z---->>>>short __stdcall DrfGetFirmwareVersion(void*, unsigned char*, unsigned char*, unsigned char)
Suzan YILDIRIM
P.S."*" isn't seen the first parameter of the function..it could be as follows; void*
Suzan YILDIRIM
You'll need to use the DllImport.ExactSpelling property in your declaration.
Hans Passant
i used it as follows; ExactSpelling=false and charset=charset.Ansi
Suzan YILDIRIM
Oops, I meant EntryPoint. Sorry.
Hans Passant
ok..i used as follows---->>> [DllImport("DrfApiV10.dll", CallingConvention = CallingConvention.StdCall,CharSet=CharSet.Ansi,EntryPoint="DrfGetFirmwareVersion", ExactSpelling=false)]
Suzan YILDIRIM
or instead of the function name i used the ordinal as follows--->>> [DllImport("DrfApiV10.dll", CallingConvention = CallingConvention.StdCall,CharSet=CharSet.Ansi,EntryPoint="#12", ExactSpelling=false)]
Suzan YILDIRIM
or like this--->>> [DllImport("DrfApiV10.dll", CallingConvention = CallingConvention.StdCall,CharSet=CharSet.Ansi,EntryPoint="?DrfGetFirmwareVersion@@YGFPAXPAE1E@Z", ExactSpelling=false)] ???
Suzan YILDIRIM
ExactSpelling=true. Use Dumpbin.exe /exports on the DLL to find the real export names. What is the error you get? Also, you should have linked your MSDN forum thread.
Hans Passant
ok..ExactSpelling=true.i have used Dumpbin.exe /exports on the DLL to find the real export names like you said. i know the ordinal of the function from there.yes i have been writing MSDN forum and i think you are a MVP on MSDN forum. what i try to do is to import the DLL into C#.it's for my graduation thesis..i'm not sure whether i marshal data types truely or not..
Suzan YILDIRIM
I'll twist your arm one more time: what is the error you get?
Hans Passant
sorry..i'm not getting any error at the moment.. because what i do is to write the managed signatures for the functions..i haven't used the imported functions yet :S i have't sent any data to the function yet..
Suzan YILDIRIM
Sigh. Well, you got more than enough info to troubleshoot it when you do.
Hans Passant
ok i will do..after then can i continue this thread ?? thank you for your feedbacks and patience..
Suzan YILDIRIM
A: 

i have used overloed for the default parameter(VS2005).i have imported the function as follows;

public class sDrfGetFirmwareVersion
        {
            //apiStatus __declspec(dllexport) __stdcall DrfGetFirmwareVersion (HANDLE hCom, unsigned char *major,unsigned char *minor, unsigned char ReaderAddr = 0xff);

            [DllImport("DrfApiV10.dll", CallingConvention = CallingConvention.StdCall,CharSet=CharSet.Ansi,EntryPoint="DrfGetFirmwareVersion", ExactSpelling=false)]
            public static extern short DrfGetFirmwareVersion(IntPtr hCom, out byte major, out byte minor,byte ReaderAddr);
            public static short DrfGetFirmwareVersion(IntPtr hCom, out byte major,out byte minor)
              {
                  return DrfGetFirmwareVersion(hCom, out major,out minor, 0xff);
              }
        }

can you verify me please ??

Suzan YILDIRIM