Click here to Skip to main content
15,888,293 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting exception on my C# dll call for
C#
[DllExport(CallingConvention = CallingConvention.Cdecl)]
        public static int SetSiteInterface(IntPtr siteInterfacePtr)
        {
            try
            {
                gSite = (SiteInterface)Marshal.PtrToStructure(siteInterfacePtr, typeof(SiteInterface));
            }
            catch(Exception ex)
            {
                
            }
            return 1;
        }


as PInvoke restriction: cannot return variants. PtrToStructure is not working.

gSite is SiteInterface as follows:

C#
[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public struct SiteInterface
    {
        public int nStructSize;
        public delegate int GetArraySizeDelegate();
        public GetArraySizeDelegate GetArraySize;
        //C++ TO C# CONVERTER WARNING: C# has no equivalent to methods returning pointers to value types:
        //ORIGINAL LINE: float * (*GetStockArray)(int nType);
        public delegate float GetStockArrayDelegate(int nType);
        public GetStockArrayDelegate GetStockArray;
        public delegate AmiVar GetVariableDelegate(string pszName);
        public GetVariableDelegate GetVariable;
        public delegate void SetVariableDelegate(string pszName, AmiVar newValue);
        public SetVariableDelegate SetVariable;
        public delegate AmiVar CallFunctionDelegate(string szName, int nNumArgs, AmiVar ArgsTable);
        public CallFunctionDelegate CallFunction;
        public delegate AmiVar AllocArrayResultDelegate();
        public AllocArrayResultDelegate AllocArrayResult;
        public delegate object AllocDelegate(uint nSize);
        public AllocDelegate Alloc;
        public delegate void FreeDelegate(object pMemory);
        public FreeDelegate Free;
        public delegate int GetDateTimeArrayDelegate(); // new in 5.30
        public GetDateTimeArrayDelegate GetDateTimeArray;
    }


The above site interface C# code was converted from C++ code as shown below:

C++
struct SiteInterface
{
                int         nStructSize;
                int         (*GetArraySize) (void);
                float *     (*GetStockArray)( int nType );
                AmiVar      (*GetVariable) ( const char *pszName );
                void        (*SetVariable) ( const char *pszName, AmiVar newValue );
                AmiVar      (*CallFunction) ( const char *szName, int nNumArgs, AmiVar *ArgsTable );
                AmiVar      (*AllocArrayResult) (void);
                void *      (*Alloc) (unsigned int nSize);
                void        (*Free) (void *pMemory);
                DATE_TIME_INT* (*GetDateTimeArray) (void);    // new in 5.30
};


C++
PLUGINAPI int SetSiteInterface( struct SiteInterface *pInterface )
{
	gSite = *pInterface;

	return TRUE;
}


What am I doing wrong here? Please let me know if you have answer.
Posted
Updated 22-Nov-16 4:10am
v2
Comments
Sergey Alexandrovich Kryukov 23-Jun-15 2:31am    
I would say, "wrong" it the whole invention of variant types. I call it 3rd worse invention (approximately, and only counting widespread conceptions) in the whole history of computing. This is not your fault, but I would advise to avoid variants.
—SA
Nilesh bhope 23-Jun-15 2:39am    
so there is no equivalent solution in C#?
Sergey Alexandrovich Kryukov 23-Jun-15 2:45am    
I don't think so, and I think this is the reason: developers of .NET are not so stupid as the people who invented the variant types. So, .NET creators probably used the same principle I suggest: avoiding dealing with variant types. I would say, this is a wise decision. If someone knows the way to deal with them, let this person correct me. I doubt that a clear and reliable solution exists.

It's known that variant is marshaled as System.Object: https://msdn.microsoft.com/en-us/library/aa719104%28VS.71%29.aspx.

Perhaps you can use reflection on the object type and pull its members and then extract the variant data itself. I would not waste time on this...

—SA
Nilesh bhope 23-Jun-15 2:55am    
I am agree with your suggestion. Thanks. I will look into it.
Sergey Alexandrovich Kryukov 23-Jun-15 2:56am    
You are welcome.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900