Need DllImport expert to convert c++ to c# - same struct pointer in struct 
Author Message
 Need DllImport expert to convert c++ to c# - same struct pointer in struct

I have read all the msdn samples about DllImport but still fail to convert
the following c++ code to C#.
The main problem is that there is a same struct pointer inside the struct.

////////////////////C++///////////////////////////
typedef struct Policy_s
{
    int             iStructId;
    int             iObjectId;
    char            pszOid[1024];
    struct Policy_s *next;

Quote:
} Policy_t;

int __stdcall GetObjects (
        void* pHandle,
        const long nObjectId,
        Policy_t** ppstructObject);
///////////////////////////////////////////////////////

The following is my conversion.

////////////////////////////////c#  that don't
work///////////////////////////////////////////

[DllImport("API.dll", EntryPoint="GetObjects",  SetLastError=true,
ExactSpelling=false)]
  unsafe public static extern int GetObjects ([In,Out] void* pHandle,
        long ObjectID,
        out IntPtr ptrObject);

[ StructLayout( LayoutKind.Sequential )]
unsafe public struct Policy_t
{
   public long iStructId;
   public long iObjectId;
   [ MarshalAs( UnmanagedType.ByValTStr, SizeConst=1024 )]
   public string pszOid
   public IntPtr next;

Quote:
}

unsafe public int GetGlobalObj()
{
// The pHandle is obtained from else where

 Policy_t pObject=new Policy_t();
 IntPtr ptrObject=Marshal.AllocCoTaskMem(Marshal.SizeOf(pObject));
 Marshal.StructureToPtr(pObject, ptrObject ,false);

 int iApiCode = 0;

 iApiCode = GetObjects(
  pHandle,
  10,
  out ptrObject);
 //Unhandled Exception: System.NullReferenceException: Object reference not
set to an instance of an object.

Quote:
}

////////////////////////////////////////////////////////////////////////////
////

I know it is not easy but please help.

Awe



Sun, 06 Nov 2005 19:39:37 GMT  
 Need DllImport expert to convert c++ to c# - same struct pointer in struct
Some one has kindly pointed me that I have to use int instead of long.
However, it is still not working. I have tried to use Policy_t* next inside
Policy_t but C# do not allow me to do this.

[ StructLayout( LayoutKind.Sequential )]
unsafe public struct Policy_t
{
   public long(change to int) iStructId;
   public long(change to int) iObjectId;
    [ MarshalAs( UnmanagedType.ByValTStr, SizeConst=1024 )]
    public string pszOid
    public IntPtr next;
 }


Quote:
> I have read all the msdn samples about DllImport but still fail to convert
> the following c++ code to c#.
> The main problem is that there is a same struct pointer inside the struct.

> ////////////////////C++///////////////////////////
> typedef struct Policy_s
> {
>     int             iStructId;
>     int             iObjectId;
>     char            pszOid[1024];
>     struct Policy_s *next;
> } Policy_t;

> int __stdcall GetObjects (
>         void* pHandle,
>         const long nObjectId,
>         Policy_t** ppstructObject);
> ///////////////////////////////////////////////////////

> The following is my conversion.

> ////////////////////////////////c#  that don't
> work///////////////////////////////////////////

> [DllImport("API.dll", EntryPoint="GetObjects",  SetLastError=true,
> ExactSpelling=false)]
>   unsafe public static extern int GetObjects ([In,Out] void* pHandle,
>         long ObjectID,
>         out IntPtr ptrObject);

> [ StructLayout( LayoutKind.Sequential )]
> unsafe public struct Policy_t
> {
>    public long iStructId;
>    public long iObjectId;
>    [ MarshalAs( UnmanagedType.ByValTStr, SizeConst=1024 )]
>    public string pszOid
>    public IntPtr next;
> }

> unsafe public int GetGlobalObj()
> {
> // The pHandle is obtained from else where

>  Policy_t pObject=new Policy_t();
>  IntPtr ptrObject=Marshal.AllocCoTaskMem(Marshal.SizeOf(pObject));
>  Marshal.StructureToPtr(pObject, ptrObject ,false);

>  int iApiCode = 0;

>  iApiCode = GetObjects(
>   pHandle,
>   10,
>   out ptrObject);
>  //Unhandled Exception: System.NullReferenceException: Object reference
not
> set to an instance of an object.

> }

////////////////////////////////////////////////////////////////////////////

- Show quoted text -

Quote:
> ////

> I know it is not easy but please help.

> Awe



Sun, 06 Nov 2005 21:33:54 GMT  
 Need DllImport expert to convert c++ to c# - same struct pointer in struct
I don't even bother converting structures all I do is :

//Header SomeClass.h
public __gc class SomeClass
{

private :
   Policy_t *policy_t;

Quote:
}

//SomeClass.cpp
SomeClass::SomeClass()
{
   policy_t = new Policy_t();

Quote:
}

SomeClass::SomeClass()
{
   delete policy_t;

Quote:
}

Forget about using dlimport and just include the lib in linker options, and
call the function directly.

Regards.


Quote:
> I have read all the msdn samples about DllImport but still fail to convert
> the following c++ code to c#.
> The main problem is that there is a same struct pointer inside the struct.

> ////////////////////C++///////////////////////////
> typedef struct Policy_s
> {
>     int             iStructId;
>     int             iObjectId;
>     char            pszOid[1024];
>     struct Policy_s *next;
> } Policy_t;

> int __stdcall GetObjects (
>         void* pHandle,
>         const long nObjectId,
>         Policy_t** ppstructObject);
> ///////////////////////////////////////////////////////

> The following is my conversion.

> ////////////////////////////////c#  that don't
> work///////////////////////////////////////////

> [DllImport("API.dll", EntryPoint="GetObjects",  SetLastError=true,
> ExactSpelling=false)]
>   unsafe public static extern int GetObjects ([In,Out] void* pHandle,
>         long ObjectID,
>         out IntPtr ptrObject);

> [ StructLayout( LayoutKind.Sequential )]
> unsafe public struct Policy_t
> {
>    public long iStructId;
>    public long iObjectId;
>    [ MarshalAs( UnmanagedType.ByValTStr, SizeConst=1024 )]
>    public string pszOid
>    public IntPtr next;
> }

> unsafe public int GetGlobalObj()
> {
> // The pHandle is obtained from else where

>  Policy_t pObject=new Policy_t();
>  IntPtr ptrObject=Marshal.AllocCoTaskMem(Marshal.SizeOf(pObject));
>  Marshal.StructureToPtr(pObject, ptrObject ,false);

>  int iApiCode = 0;

>  iApiCode = GetObjects(
>   pHandle,
>   10,
>   out ptrObject);
>  //Unhandled Exception: System.NullReferenceException: Object reference
not
> set to an instance of an object.

> }

////////////////////////////////////////////////////////////////////////////

- Show quoted text -

Quote:
> ////

> I know it is not easy but please help.

> Awe



Mon, 07 Nov 2005 12:20:44 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. pointers to functions in structs which take a pointer to that struct as an arg

2. cast pointer to struct to pointer to type of first member of struct

3. expert on MALLOC AND STRUCTs needed now!

4. struct.array works - struct->pointer not

5. Assign struct to struct via pointer

6. pass struct or pointer to struct ???

7. return a struct, not a struct pointer ?

8. Why pass structs? (not struct pointers)

9. pointer in struct that references a struct

10. c# struct vs c++ struct

11. Need help converting from tm struct to DATE

12. convert C++ class into C struct

 

 
Powered by phpBB® Forum Software