.net 平台调用

总结:
1.参数传递

@param [in] ellipsoid 指针
@param [in] prj           指针
@param [in] uPnts     数组指针
@param [in] count     简单类型
@param [in] blCoords 传出数组指针
extern "C" GEO_API void __stdcall GaussProjInvert(const Ellipsoid* ellipsoid, const Prj* prj, const UPnt* uPnts, int count, BLCoord* blCoords);

[DllImport("Geo.dll",CallingConvention = CallingConvention.StdCall)]
static extern void GaussProj(ref Ellipsoid ellipsoid, ref Prj prj,   BLCoord[] blCoords, int count,  [Out] UPnt[] uPnts);

网址 :http://www.pinvoke.net/
2. 接口调用
#ifndef __HEADER_DLL__
#define  __HEADER_DLL__

#include 
< windows.h >
#ifdef DLL_EXPORTS
#define  DLL_API __declspec(dllexport)
#else
#define  DLL_API __declspec(dllimport)
#endif

#endif // __HEADER_DLL__

#ifndef __HEADER_INTIMPL_DLL__
#define  __HEADER_INTIMPL_DLL__
#include 
" Int.h "
namespace  Dll
{
    
class IntImp : public Int, public Int1, public Int2                                        
       
{
        
public:
        IntImp(
int val);
        
virtual int Sub(int a);
        
virtual int Mul(int a);
        
virtual bool GetInterface(void** pptr, int iid);
        
virtual void Delete();
        
int val;
    }
;
}

#endif // __HEADER_INTIMPL_DLL__

#ifndef __HEADER_INT_DLL__
#define  __HEADER_INT_DLL__
#include 
" Dll.h "
namespace  Dll
{
    
class Int
    
{
        
public:
        
virtual bool GetInterface(void** pptr, int iid) = 0;
        
virtual void Delete() = 0;
    }
;
    
class Int1
    
{
        
public:
        
virtual int Sub(int a) = 0;
    }
;
    
class Int2
    
{
        
public:
        
virtual int Mul(int b) = 0;
    }
;
    
extern "C" DLL_API Int*  __stdcall CreateInt(int a);
    
extern "C" DLL_API void  __stdcall InvokeIntDelete(Int* in);
    
extern "C" DLL_API void  __stdcall InvokeIntGetInterface(Int* invoid** pptr, int iid);
    
extern "C" DLL_API int   __stdcall InvokeInt1Sub(Int1* inint a);
    
extern "C" DLL_API int   __stdcall InvokeInt2Mul(Int2* inint a);


}

#endif // __HEADER_INT_DLL__

#include 
" IntImpl.h "
namespace  Dll
{

IntImp::IntImp(
int val)
{
    
this->val = val;
}

int IntImp::Sub(int a)
{
    
return this->val-a;

}

int IntImp::Mul(int a)
{
    
return this->val*a;

}

bool IntImp::GetInterface(void** pptr, int iid)
{
    
bool res = false;

    
if ( iid == 1 )
    
{
        
*pptr = (Int1*)this;
        res 
= true;
    }

        
else if ( iid == 2 )
    
{
        
*pptr = (Int2*)this;
        res 
= true;
    }


    
return res;

}


void IntImp::Delete()
{
    delete 
this;
}


extern "C" DLL_API Int* __stdcall CreateInt(int a)
{
    
return new IntImp(a);

}


extern "C" DLL_API void  __stdcall InvokeIntDelete(Int* in)
{
    
in->Delete();

}


extern "C" DLL_API void  __stdcall InvokeIntGetInterface(Int* invoid** pptr, int iid)
{
    
in->GetInterface(pptr, iid);
}


extern "C" DLL_API int  __stdcall InvokeInt1Sub(Int1* inint a)
{
    
return in->Sub(a);

}


extern "C" DLL_API int  __stdcall InvokeInt2Mul(Int2* inint a)
{
    
return in->Mul(a);
}



}


using  System;
using  System.Runtime.InteropServices;
namespace  DLL
{
    
public class Wrapper
    
{
        [DllImport(
"Dll.dll",CallingConvention = CallingConvention.StdCall)]
        
public static extern IntPtr CreateInt(int a);
        [DllImport(
"Dll.dll",CallingConvention = CallingConvention.StdCall)]
        
public static extern int InvokeIntDelete(IntPtr pPtr);
        [DllImport(
"Dll.dll",CallingConvention = CallingConvention.StdCall)]
        
public static extern bool InvokeIntGetInterface(IntPtr ptr, [Out] IntPtr[] pPtr, int iid);
        [DllImport(
"Dll.dll",CallingConvention = CallingConvention.StdCall)]
        
public static extern int InvokeInt1Sub(IntPtr pPtr, int a);
        [DllImport(
"Dll.dll",CallingConvention = CallingConvention.StdCall)]
        
public static extern int InvokeInt2Mul(IntPtr pPtr, int a);
    }

    
public class Int
    
{
        
private IntPtr target;
        
public  Int( int a )
        
{
            target 
= Wrapper.CreateInt(a);
        }

        
public object GetInterface( int iid )
        
{
            IntPtr[] pPtr 
= new IntPtr[1];
            
            
if ( Wrapper.InvokeIntGetInterface( target, pPtr, iid) && iid == 1 )
            
{
                
return new Int1(pPtr[0]);
            }


            
if ( Wrapper.InvokeIntGetInterface( target, pPtr, iid) && iid == 2 )
            
{
                
return new Int2(pPtr[0]);
            }


            
return null;
        }

        
public void Delete()
        
{
            Wrapper.InvokeIntDelete(target);
        }

    }

    
public class Int1
    
{
        
private IntPtr target;
        
public  Int1( IntPtr target )
        
{
            
this.target = target;
        }

        
public int Sub(int a)
        
{
            
return Wrapper.InvokeInt1Sub(target, a);
        }

    }

    
public class Int2
    
{
        
private IntPtr target;
        
public  Int2( IntPtr target )
        
{
            
this.target = target;
        }

        
public int Mul(int a)
        
{
            
return Wrapper.InvokeInt2Mul(target, a);
        }

    }

    
}

3.this is not com
#ifndef __HEADER_ENTITY_DLL__
#define  __HEADER_ENTITY_DLL__
#include 
" Dll.h "
namespace  Dll
{
const int DLL_ENTITY = 0x0;
class Entity
{
public:
    
virtual bool QueryInterface(void** pptr, int iid) = 0;
    
virtual void AddRef() = 0;
    
virtual void Release() = 0;
}
;
const int DLL_ADDENTITY = 0x01;
class AddEntity : public Entity
{
public:
    
virtual int Add(int dest) = 0;
}
;
const int DLL_SUBENTITY = 0x02;
class SubEntity : public Entity
{
public:
    
virtual int Sub(int dest) = 0;
}
;
const int DLL_MULENTITY = 0x03;
class MulEntity : public Entity
{
public:
    
virtual int Mul(int dest) = 0;
}
;

extern "C" DLL_API AddEntity*  __stdcall CreateAddEntity(int val);
extern "C" DLL_API SubEntity*  __stdcall CreateSubEntity(int val);
extern "C" DLL_API MulEntity*  __stdcall CreateMulEntity(int val);

extern "C" DLL_API int   __stdcall Invoke_AddEntity_Add(AddEntity* entity, int dest);
extern "C" DLL_API int   __stdcall Invoke_SubEntity_Sub(SubEntity* entity, int dest);
extern "C" DLL_API int   __stdcall Invoke_MulEntity_Mul(MulEntity* entity, int dest);

extern "C" DLL_API bool  __stdcall Invoke_Entity_QueryInterface(Entity* entity, void** pPtr, int iid);
extern "C" DLL_API void  __stdcall Invoke_Entity_AddRef(Entity* entity);
extern "C" DLL_API void  __stdcall Invoke_Entity_Release(Entity* entity);


}

#endif // __HEADER_ENTITY_DLL__

#ifndef __HEADER_ENTITYIMPL_DLL__
#define  __HEADER_ENTITYIMPL_DLL__
#include 
" Entity.h "
namespace  Dll
{
class EntityImpl : public AddEntity, public SubEntity, public MulEntity
{
public:
    EntityImpl(
int val);

    
virtual bool QueryInterface(void** pptr, int iid);
    
virtual void AddRef();
    
virtual void Release();

    
virtual int Add(int dest);

    
virtual int Sub(int dest);

    
virtual int Mul(int dest);

    
int val;
    
int refCount;

}
;
}

#endif // __HEADER_ENTITYIMPL_DLL__

#include 
" EntityImpl.h "
namespace  Dll
{

EntityImpl::EntityImpl(
int val)
{
    
this->val = val;
    refCount 
= 0;

}


bool EntityImpl::QueryInterface(void** pptr, int iid)
{
    
bool res = false;
    
switch( iid )
    
{
    
case DLL_ENTITY : 
        
{
            
*pptr = (AddEntity*)this;
            res 
= true;
        }

        
break;
    
case DLL_ADDENTITY : 
        
{
            
*pptr = (AddEntity*)this;
            res 
= true;
        }

        
break;
    
case DLL_SUBENTITY : 
        
{
            
*pptr = (SubEntity*)this;
            res 
= true;
        }

        
break;
    
case DLL_MULENTITY : 
        
{
            
*pptr = (MulEntity*)this;
            res 
= true;
        }

        
break;
    
default:
        
break;
    }


    
return res;

}


void EntityImpl::AddRef()
{
    
++refCount;

}


void EntityImpl::Release()
{
    
--refCount;
    
if ( refCount == 0)
    
{
        delete 
this;
    }

}


int EntityImpl::Add(int dest)
{
    
return val+dest;

}


int EntityImpl::Sub(int dest)
{
    
return val-dest;

}


int EntityImpl::Mul(int dest)
{
    
return val*dest;

}


extern "C" DLL_API AddEntity*  __stdcall CreateAddEntity(int val)
{
    
return (AddEntity*)new EntityImpl(val);

}


extern "C" DLL_API SubEntity*  __stdcall CreateSubEntity(int val)
{
    
return (SubEntity*)new EntityImpl(val);

}


extern "C" DLL_API MulEntity*  __stdcall CreateMulEntity(int val)
{
    
return (MulEntity*)new EntityImpl(val);
}


extern "C" DLL_API int  __stdcall Invoke_AddEntity_Add(AddEntity* entity, int dest)
{
    
return entity->Add(dest);

}


extern "C" DLL_API int  __stdcall Invoke_SubEntity_Sub(SubEntity* entity, int dest)
{
    
return entity->Sub(dest);

}


extern "C" DLL_API int   __stdcall Invoke_MulEntity_Mul(MulEntity* entity, int dest)
{
    
return entity->Mul(dest);

}


extern "C" DLL_API bool  __stdcall Invoke_Entity_QueryInterface(Entity* entity, void** pPtr, int iid)
{
    
return entity->QueryInterface(pPtr, iid);

}


extern "C" DLL_API void  __stdcall Invoke_Entity_AddRef(Entity* entity)
{
    entity
->AddRef();

}


extern "C" DLL_API void   __stdcall Invoke_Entity_Release(Entity* entity)
{
    entity
->Release();

}


}


using  System;
using  System.Runtime.InteropServices;
namespace  DLL
{
    
public class Wrapper
    
{
        
public const int DLL_ENTITY    = 0x0;
        
public const int DLL_ADDENTITY = 0x01;
        
public const int DLL_SUBENTITY = 0x02;
        
public const int DLL_MULENTITY = 0x03;

        [DllImport(
"Dll.dll",CallingConvention = CallingConvention.StdCall)]
        
public static extern IntPtr CreateAddEntity(int val);
        [DllImport(
"Dll.dll",CallingConvention = CallingConvention.StdCall)]
        
public static extern IntPtr CreateSubEntity(int val);
        [DllImport(
"Dll.dll",CallingConvention = CallingConvention.StdCall)]
        
public static extern IntPtr CreateMulEntity(int val);


        [DllImport(
"Dll.dll",CallingConvention = CallingConvention.StdCall)]
        
public static extern bool Invoke_Entity_QueryInterface(IntPtr ptr, [Out] IntPtr[] pPtr, int iid);
        [DllImport(
"Dll.dll",CallingConvention = CallingConvention.StdCall)]
        
public static extern int Invoke_Entity_AddRef(IntPtr pPtr);
        [DllImport(
"Dll.dll",CallingConvention = CallingConvention.StdCall)]
        
public static extern int Invoke_Entity_Release(IntPtr pPtr);

        [DllImport(
"Dll.dll",CallingConvention = CallingConvention.StdCall)]
        
public static extern int Invoke_AddEntity_Add(IntPtr pPtr, int a);
        [DllImport(
"Dll.dll",CallingConvention = CallingConvention.StdCall)]
        
public static extern int Invoke_SubEntity_Sub(IntPtr pPtr, int a);
        [DllImport(
"Dll.dll",CallingConvention = CallingConvention.StdCall)]
        
public static extern int Invoke_MulEntity_Mul(IntPtr pPtr, int a);
    }

    
public class Entity
    
{
        
protected IntPtr target;
        
public  Entity( IntPtr target )
        
{
            
this.target = target;
        }

        
public object GetInterface( int iid )
        
{
            IntPtr[] pPtr 
= new IntPtr[1];
            
object obj = null;
            
if ( Wrapper.Invoke_Entity_QueryInterface(target, pPtr, iid) )
            
{
                
switch ( iid )
                
{
                
case Wrapper.DLL_ENTITY :
                    obj 
= new Entity(pPtr[0]);
                    
break;
                
case Wrapper.DLL_ADDENTITY :
                    obj 
= new AddEntity(pPtr[0]);
                    
break;
                
case Wrapper.DLL_SUBENTITY :
                    obj 
= new SubEntity(pPtr[0]);
                    
break;
                
case Wrapper.DLL_MULENTITY :
                    obj 
= new MulEntity(pPtr[0]);
                    
break;
                
default :
                    
break;
                }
    
            }


            
return obj;
        }


        
public void AddRef()
        
{
            Wrapper.Invoke_Entity_AddRef(target);

        }


        
public void Release()
        
{
            Wrapper.Invoke_Entity_Release(target);

        }


    }

public class SubEntity : Entity
{

public SubEntity( IntPtr target ) : base(target)
{

}


public SubEntity( int val ) : base(IntPtr.Zero)  
{
    
this.target = Wrapper.CreateSubEntity(val);
}


public int Sub(int a)
{
    
return Wrapper.Invoke_SubEntity_Sub(target, a);
}

}

public class AddEntity : Entity
{
public AddEntity( IntPtr target ) : base(target)
{

}

public AddEntity( int val) : base(IntPtr.Zero)
{
    
this.target = Wrapper.CreateAddEntity(val);

}


public int Add(int a)
{
    
return Wrapper.Invoke_AddEntity_Add(target, a);
}

}

public class MulEntity : Entity
{
public MulEntity( IntPtr target ) : base(target)
{

}


public MulEntity( int val ) : base(IntPtr.Zero)
{
    
this.target = Wrapper.CreateMulEntity(val);
}


public int Mul(int a)
{
    
return Wrapper.Invoke_MulEntity_Mul(target, a);
}

}

}



更新中。。。。。。。。。。。。。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值