3rd party dll interface to c++/cli .net 2.x

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • microface
    New Member
    • Apr 2010
    • 1

    3rd party dll interface to c++/cli .net 2.x

    Yes I have googled, and found lots of supposed answers to this problem of using P/Invoke but everything I found, and everything I tried either caused exceptions, or simply failed to fill in the struct.
    So here is my problem
    Code:
    typedef struct tagEdsDirectoryItemInfo
    {
        EdsUInt32   size;
        EdsBool     isFolder;
        EdsUInt32   groupID;
        EdsUInt32   option;
        EdsChar     szFileName[ EDS_MAX_NAME ];
    
    } EdsDirectoryItemInfo;
    
    unsigned int EDSAPI EdsGetDirectoryItemInfo(
                                    EdsDirectoryItemRef    inDirItemRef,
                                    EdsDirectoryItemInfo*   outDirItemInfo );
    
    
     /*-----------------------------------------------------------------------------
             DirectoryItem Info
            -----------------------------------------------------------------------------*/
            [StructLayout(LayoutKind::Sequential)]
            value struct EdsDirectoryItemInfo 
            {
                unsigned int Size;
                int  isFolder;
                unsigned int GroupID;
                unsigned int Option;
                [MarshalAs(UnmanagedType::LPArray,SizeConst=EDS_MAX_NAME)]
                cli::array<char> ^szFileName;
                
                //[MarshalAs( UnmanagedType::ByValTStr, SizeConst=EDS_MAX_NAME)]
                //System::Text::StringBuilder ^  szFileName;
                //[MarshalAs(UnmanagedType::AsAny,SizeConst=EDS_MAX_NAME)]
                //cli::array<char> ^szFileName;
                //EdsDirectoryItemInfo():szFileName(gcnew cli::array<char>(EDS_MAX_NAME+1)){;};
                
            };
    
            [System::Runtime::InteropServices::DllImportAttribute("EDSDK.dll",CallingConvention = CallingConvention::Cdecl)]
            static  unsigned int EdsGetDirectoryItemInfo( IntPtr inDirItemRef,
                 EdsDirectoryItemInfo^ outDirItemInfo);
    //        static  unsigned int EdsGetDirectoryItemInfo( IntPtr inDirItemRef,
    //             IntPtr outDirItemInfo);
    
    I am calling this dll exported function in this Canon SDK from a delegate.
    The delegate is called, and depending on what I do I can get exception from the 
    marshalling, or err is 0 and nothing is filled in the outDirItemInfo
    
    inRef is an IntPtr handle to some item in the 3rd party Canon SDK
    
    
    
    Actual Function Calls
    EdsDirectoryItemInfo^	dirItemInfo = gcnew EdsDirectoryItemInfo();
    				dirItemInfo->szFileName = gcnew cli::array<char>EDS_MAX_NAME+1);
    unsigned int err;
    				
    //IntPtr ptr;
    //ptr = Marshal::AllocHGlobal(Marshal::SizeOf(dirItemInfo));
    
    err = EdsGetDirectoryItemInfo( inRef,dirItemInfo);
    I have tried many different types of marshalling, I have tried to make the dirItemInfo and IntPtr,
    I am just very frustrated, after 3 days of futzing around with this I am just at the point of telling my boss it can not be done, and just write the code in gold old MFC.

    Oh in the process the Canon SDK provided a C# class which was filled with errors that I had to fix, by looking at their C example.
Working...