1

I am trying to convert a driver client from c to c#. I am able to make it work completely in c but as I am converting it in c# it is blocking the file to open the code for your reference is as follows:

       [DllImport("fltLib.dll", CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)]  
        public static extern unsafe int FilterGetMessage(  
            IntPtr hPort,  
            IntPtr lpMessageBuffer,  
            int dwMessageBufferSize,  
            void* Overlapped  
            );  
    IntPtr buf = Marshal.AllocHGlobal(
         Marshal.SizeOf(msg.MessageHeader));
        OVERLAPPED povlp;
         Marshal.StructureToPtr(msg.MessageHeader, buf, false);
         var sizeUserRec = Marshal.SizeOf(typeof(OVERLAPPED));
         var userRec = Marshal.AllocHGlobal(sizeUserRec);
         povlp.Internal = UIntPtr.Zero;
         povlp.InternalHigh = UIntPtr.Zero;
         povlp.Offset = 0;
         povlp.OffsetHigh = 0;
         povlp.Pointer = IntPtr.Zero;
         povlp.hEvent = IntPtr.Zero;
         headerSize = Marshal.SizeOf(msg.MessageHeader);

             msgsize += headerSize;
 unsafe
             {
                 status = FilterGetMessage(
                                        portPtr,
                                        buf,
                                        msgsize,
                                        &povlp
                                        ); 
             }

[DllImport("kernel32.dll")] static extern bool GetQueuedCompletionStatus(IntPtr CompletionPort, ref uint lpNumberOfBytes, ref IntPtr lpCompletionKey, ref IntPtr lpOverlapped, uint dwMilliseconds);

 static _OVERLAPPED Ovlp;  

 Console.WriteLine("int sddc");
        _SCANNER_NOTIFICATION notification;
        _SCANNER_REPLY_MESSAGE replyMessage;        
        IntPtr pOvlp;
        pOvlp = Marshal.AllocHGlobal(Marshal.SizeOf(Ovlp));
        Marshal.StructureToPtr(Ovlp, pOvlp, false);
        _SCANNER_MESSAGE message;                
         bool result;
         uint outSize = 3435973836, milisec = 0xFFFFFFFF;
         int hr;
         IntPtr key =  IntPtr.Zero;             
         result = GetQueuedCompletionStatus(Context.Completion, ref outSize, ref key, ref pOvlp,uint.MaxValue); //error line
         if (!result)
         {
             Console.WriteLine("Not valid");
         }
  • The dll should run exactly the same in C# and C++. So the parameters you are passing a different. Was the dll built as a 16 bit or 32 bit application? I don't think the dll is blocking. I just think the time parameter is different or Context.Completion is different. You did not include the type for Context.Completion. – jdweng May 01 '15 at 06:36
  • @jdweng I am checking it out but I am not getting exactly where is the problem, I have debug the code of c++ that is running fine with driver and got that before calling GetQueuedCompletionStatus it is calling FilterGetMessage and here also I am getting the error should I include that to give you reference??? –  May 02 '15 at 05:34
  • The stack trace of the exception would help. I normally step into the c++ code a verify the parameters are correct. You may just need to specify c calling convention. See following webpage : http://stackoverflow.com/questions/5938416/accessviolationexception-when-pinvoking-c-dll-cdecl-calling-convention-proble – jdweng May 02 '15 at 18:00
  • @jdweng this link not helping much about this I did some debugging again I found that its the problem of FilterGetMessage function it is returning some negetive values should I include that code for reference of yours?? –  May 04 '15 at 06:28
  • Any more info would help – jdweng May 04 '15 at 07:00
  • @jdweng please refer the edited code –  May 04 '15 at 07:14
  • You function definition doesn't agree with the following webpage : http://www.pinvoke.net/default.aspx/kernel32.GetQueuedCompletionStatus – jdweng May 04 '15 at 07:25
  • @jdweng okay but what's about fltgetmessage function this one is not working and giving a negetive value –  May 04 '15 at 07:30
  • Solve one issue at a time. Direction of variables may be causing issue. I sometimes use getlasterror to give more info. See webpage : http://www.pinvoke.net/default.aspx/coredll/GetLastError.html – jdweng May 04 '15 at 07:33
  • @jdweng both GetLastError() and Marshal.GetLastWin32Error() are giving output as 0 –  May 04 '15 at 07:38
  • Which error occurs first )GetQueuedCompletionStatus or filterGetMessage)? di you put GetLatError immediately after the failing method? I normally step into the function in both C# and C++ and compare the parameter list to determine which parameters don't match. C calling convention pushes last parameter into stack first. So the last parameter that don't match should be fixed first. – jdweng May 04 '15 at 10:45
  • @jdweng okay let me check it again –  May 04 '15 at 11:30

0 Answers0