CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2008
    Posts
    78

    Calling unmanaged dll throwing PInvoke exception

    Hi,

    I have an unmanaged dll and I am trying to make a wrapper for it in C#. Although everything seems alright but I am getting Exception:

    Code:
     DLL code
    
    EXPORT enum e_error_codes Link(char *, enum e_baudrates);
    enum e_error_codes {
            ERR_NONE =                  (ERR_CATEGORY_GENERAL | 0x00),  
            ERR_WRONG_CRC =             (ERR_CATEGORY_GENERAL | 0x04),  
                                };
    enum e_baudrates {
            BAUDRATE_1200 = 1,                     
            BAUDRATE_2400 = 2,                     
                             };
    
     Wrapper code
    
    [DllImport(@"DolDevice.dll")]
            static extern Errors.e_error_codes Link(String aComPort, t_eBaudrate aBaudrate);
            public enum t_eBaudrate
            {
                BAUDRATE_1200 = 1,                  
                BAUDRATE_2400 = 2                   
             }
            public enum e_error_codes
            {
                ERR_NONE = (ERR_CATEGORY_GENERAL | 0x00),  
                ERR_WRONG_CRC = (ERR_CATEGORY_GENERAL | 0x04)  
             }
    
            public Errors.e_error_codes InitDevice(string aComPort)
            {
                return Link(aComPort, t_eBaudrate.BAUDRATE_1200);       
            }
    I have tried most common things while calling InitDevice like replacing replacing char* with MarshalAs(unmanaged.LPStr) etc. Replacing enum with int but getting exception all the time.
    PInvokeStackImbalance was detected
    "Message: A call to PInvoke function 'CSSsppDriver!CSSsppDriver.MainClass::Link' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature."

    I have also tried on 32-bit and 64-bit computer. The "Allow Unsafe Code" option is also checked.

    Can someone please help me where I am wrong?

  2. #2
    Join Date
    Apr 2014
    Location
    in northeast ohio
    Posts
    94

    Re: Calling unmanaged dll throwing PInvoke exception

    im certainly not experienced with pinvoke usage
    i found a number of similar posts with similar errors maybe the same solution applys

    http://stackoverflow.com/questions/3...or-turn-it-off

    if you scroll down on that post there is also link to a site that has a add on for vs that helps with pinvoke syntax
    Last edited by willmotil; September 11th, 2014 at 12:26 PM.

  3. #3
    Join Date
    Aug 2008
    Posts
    78

    Resolved Re: Calling unmanaged dll throwing PInvoke exception

    Thanks!! It solved the problem.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured