lapucca
asked on
Getting memory corruption error when I call a C-code function.
Hi, I'm using VS2005, .net 2 for windows application.
The function that I'm trying to call through DLLImport has a parameter that
has a C code's vector's Itrator to a structure. Â I Have marshalled the
structure in C# but how apparently C# doesn't have an equivalent of vector and iterator. Â
I thought I'll create another C code function like this one but using ony the structure CUserContextData as a parameter. Â I need help with the memory allocation in the program below. Â Can someone hlep me out here on how to do this correctly? Â Thank you.
I get a memory error when I try caling it righ now.
//-------- This is the C code for the import method of the dll
//extern "C" DE_ERRORS __declspec(dllexport)Encod eAsnUser(B lob** ppBlob,
//vector <CUserContextData>::iterat or userDataIter)
extern "C" DE_ERRORS __declspec(dllexport)Encod eAsnUser(B lob** ppBlob,
CUserContextData userDataIter)
{
   _bstr_t temp;
   AsnData* pAsn;
   //int boolInt;
   std::wstring wsUID;
  Â
   if ( ( pAsn = AsnAlloc( NULL ) ) == NULL )
   {
      DbgLog( DL_ERROR, "Unable to allocate ASN.1 buffer" );
      return DE_MEMORY_ALLOCATION_FAILU RE;
   }
   if ( !AsnPushTag(pAsn, (ASN_TAG$APPLICATION_START ) ) )      Â
      goto failed;
   if (!AsnWriteInteger( pAsn, REVISION))
      goto failed;   Â
   if(userDataIter->bUnifiedI D)//use UID
   {
      if (!AsnWriteInteger( pAsn, True) )
         goto failed;
   }
   else  //use indep ID
   {
      if (!AsnWriteInteger( pAsn, False) )
         goto failed;   Â
   }
   if(userDataIter->IID == NULL)
   {
      if ( !AsnWriteGeneralString( pAsn, EMPTY_STRING))
         goto failed;
   }
   else
   {
      _bstr_t bstrtUID = L"0"; //initilize the bstr
         _itow(userDataIter->IID, bstrtUID, 10);
      if ( !AsnWriteGeneralString( pAsn, bstrtUID.operator const char *()))
         goto failed;   Â
   }
  Â
   //WinUser name
   if(userDataIter->bWinLogOn )
   {
      if (!AsnWriteInteger( pAsn, True) )
         goto failed;
   }
   else//use indep User Name
   {
      if (!AsnWriteInteger( pAsn, False) )
         goto failed;      Â
   }
   //Geco
   if ( !AsnWriteGeneralString( pAsn, EMPTY_STRING))
      goto failed;
   //Shell
   temp = userDataIter->shell.c_str( );
   if ( !AsnWriteGeneralString( pAsn, temp.operator const char *()))
      goto failed;   Â
   //Home Driectory
   temp = userDataIter->homeDir.c_st r();
   if ( !AsnWriteGeneralString( pAsn, temp.operator const char *()))
      goto failed;   Â
   //Primary Group SID
   temp = userDataIter->primaryGroup SID.c_str( );
   if (!AsnWriteGeneralString( pAsn, temp.operator const char *()))
      goto failed;   Â
   if ( !AsnPopTag( pAsn ) )
      goto failed;
   *ppBlob = AsnExtractData( pAsn );
   AsnFree( pAsn );
   return DE_SUCCESS;
  Â
failed:
   DbgLog(DL_ERROR, "Failed to encode ASN.1 message packet" );
   AsnFree( pAsn );
   return DE_ENCODE_MESSAGE_PACKET_F AILURE;
}
The function that I'm trying to call through DLLImport has a parameter that
has a C code's vector's Itrator to a structure. Â I Have marshalled the
structure in C# but how apparently C# doesn't have an equivalent of vector and iterator. Â
I thought I'll create another C code function like this one but using ony the structure CUserContextData as a parameter. Â I need help with the memory allocation in the program below. Â Can someone hlep me out here on how to do this correctly? Â Thank you.
I get a memory error when I try caling it righ now.
//-------- This is the C code for the import method of the dll
//extern "C" DE_ERRORS __declspec(dllexport)Encod
//vector <CUserContextData>::iterat
extern "C" DE_ERRORS __declspec(dllexport)Encod
CUserContextData userDataIter)
{
   _bstr_t temp;
   AsnData* pAsn;
   //int boolInt;
   std::wstring wsUID;
  Â
   if ( ( pAsn = AsnAlloc( NULL ) ) == NULL )
   {
      DbgLog( DL_ERROR, "Unable to allocate ASN.1 buffer" );
      return DE_MEMORY_ALLOCATION_FAILU
   }
   if ( !AsnPushTag(pAsn, (ASN_TAG$APPLICATION_START
      goto failed;
   if (!AsnWriteInteger( pAsn, REVISION))
      goto failed;   Â
   if(userDataIter->bUnifiedI
   {
      if (!AsnWriteInteger( pAsn, True) )
         goto failed;
   }
   else  //use indep ID
   {
      if (!AsnWriteInteger( pAsn, False) )
         goto failed;   Â
   }
   if(userDataIter->IID == NULL)
   {
      if ( !AsnWriteGeneralString( pAsn, EMPTY_STRING))
         goto failed;
   }
   else
   {
      _bstr_t bstrtUID = L"0"; //initilize the bstr
         _itow(userDataIter->IID, bstrtUID, 10);
      if ( !AsnWriteGeneralString( pAsn, bstrtUID.operator const char *()))
         goto failed;   Â
   }
  Â
   //WinUser name
   if(userDataIter->bWinLogOn
   {
      if (!AsnWriteInteger( pAsn, True) )
         goto failed;
   }
   else//use indep User Name
   {
      if (!AsnWriteInteger( pAsn, False) )
         goto failed;      Â
   }
   //Geco
   if ( !AsnWriteGeneralString( pAsn, EMPTY_STRING))
      goto failed;
   //Shell
   temp = userDataIter->shell.c_str(
   if ( !AsnWriteGeneralString( pAsn, temp.operator const char *()))
      goto failed;   Â
   //Home Driectory
   temp = userDataIter->homeDir.c_st
   if ( !AsnWriteGeneralString( pAsn, temp.operator const char *()))
      goto failed;   Â
   //Primary Group SID
   temp = userDataIter->primaryGroup
   if (!AsnWriteGeneralString( pAsn, temp.operator const char *()))
      goto failed;   Â
   if ( !AsnPopTag( pAsn ) )
      goto failed;
   *ppBlob = AsnExtractData( pAsn );
   AsnFree( pAsn );
   return DE_SUCCESS;
  Â
failed:
   DbgLog(DL_ERROR, "Failed to encode ASN.1 message packet" );
   AsnFree( pAsn );
   return DE_ENCODE_MESSAGE_PACKET_F
}
Oh, by "how are you managing that iterator you pass in?" I mean how you are obtaining and storing it before you pass it to that function.
In general, I'd rather advise against marshalling a C++ construct if you are no providing some interpretation layer (line e.g. using "handle" like techniques where such a handle maps to an internal data structure and is seen as "opaque" by the caller). Any chance to redesign the whole thing and keep the iterator at the C++ side only? This would for sure make things easier.
In general, I'd rather advise against marshalling a C++ construct if you are no providing some interpretation layer (line e.g. using "handle" like techniques where such a handle maps to an internal data structure and is seen as "opaque" by the caller). Any chance to redesign the whole thing and keep the iterator at the C++ side only? This would for sure make things easier.
ASKER
Hi ikr,
Sorry, but I copied the wrong version to the question earlier. Â The original C function uses a vector iterator as a parameter and I can't use DllImport in C# to call it because C# doesn't have a vector type. Â I tried modifiying the original C code's function to accept only the structure of CUserContextData passed in as the parameter that contains the data to be encoded. Â I was able to compile it successfully in VS C++ compiler as a un-managed code. Â However, when i call this from my C# application, I would get a "memory corruption error". Â I'm able to step through this fuction and I don't know if that is becuase it's a DllImport function. Â Can you see what's wrong in my code here? Â Thank you very much.
Here is how I declare this in C#:
    public class LibWrap
    {
       [DllImport("UnityDecodeAsn User.dll", CharSet = CharSet.Unicode)]
      public static extern DE_ERRORS EncodeAsnUser(ref Blob blob,
         [In, Out]CUserContextData m);
    }
Here is my structure marshaling
    [StructLayout(LayoutKind.S equential, CharSet = CharSet.Unicode)]
    public class CUserContextData
    {
      public int bWinLogOn = 0;
      public int bUnifiedID = 0;
      [MarshalAs(UnmanagedType.B yValTStr, SizeConst = MAX_ADSPATH_CHARS)]
      public String shell = null;
      [MarshalAs(UnmanagedType.B yValTStr, SizeConst = MAX_ADSPATH_CHARS)]
      public String homeDir = null;
      [MarshalAs(UnmanagedType.B yValTStr, SizeConst = MAX_ADSPATH_CHARS)]
      public String primaryGroupSID = null;
      [MarshalAs(UnmanagedType.B yValTStr, SizeConst = MAX_ADSPATH_CHARS)]
      public String LoginName = null;
      public int symarkUID = 0; //Unified User ID
      public int IID = 0;    //Independant ID
      public int Revision = 0;
    }
Here is the c code:
extern "C" DE_ERRORS __declspec(dllexport)Encod eAsnUser(B lob** ppBlob, CUserContextData userDataIter)
{
      _bstr_t temp;
      AsnData* pAsn;
      std::wstring wsUID;
      int REVISION=0;
      const char* EMPTY_STRING;
      EMPTY_STRING = "";
      if ( ( pAsn = AsnAlloc( NULL ) ) == NULL )
      {
           DbgLog( DL_ERROR, "Unable to allocate ASN.1 buffer" );
           return DE_MEMORY_ALLOCATION_FAILU RE;
      }
      if ( !AsnPushTag(pAsn, (ASN_TAG$APPLICATION_START ) ) )           Â
           goto failed;
      if (!AsnWriteInteger( pAsn, REVISION))
           goto failed;     Â
      if(userDataIter.bUnifiedID )//use UID
      {
           if (!AsnWriteInteger( pAsn, True) )
                 goto failed;
      }
      else  //use indep ID
      {
           if (!AsnWriteInteger( pAsn, False) )
                 goto failed;     Â
      }
      if(userDataIter.IID == NULL)
      {
           if ( !AsnWriteGeneralString( pAsn, EMPTY_STRING))
                 goto failed;
      }
      else
      {
           _bstr_t bstrtUID = L"0"; //initilize the bstr
                 _itow(userDataIter.IID, bstrtUID, 10);
           if ( !AsnWriteGeneralString( pAsn, bstrtUID.operator const char *()))
                 goto failed;     Â
      }
     Â
      //WinUser name
      if(userDataIter.bWinLogOn)
      {
           if (!AsnWriteInteger( pAsn, True) )
                 goto failed;
      }
      else//use indep User Name
      {
           if (!AsnWriteInteger( pAsn, False) )
                 goto failed;           Â
      }
      //Geco
      if ( !AsnWriteGeneralString( pAsn, EMPTY_STRING))
           goto failed;
      //Shell
      temp = userDataIter.shell;
      if ( !AsnWriteGeneralString( pAsn, temp.operator const char *()))
           goto failed;     Â
      //Home Driectory
      temp = userDataIter.homeDir;
      if ( !AsnWriteGeneralString( pAsn, temp.operator const char *()))
           goto failed;     Â
      //Primary Group SID
      temp = userDataIter.primaryGroupS ID;
      if (!AsnWriteGeneralString( pAsn, temp.operator const char *()))
           goto failed;     Â
      if ( !AsnPopTag( pAsn ) )
           goto failed;
      *ppBlob = AsnExtractData( pAsn );
      AsnFree( pAsn );
      return DE_SUCCESS;
     Â
failed:
      DbgLog(DL_ERROR, "Failed to encode ASN.1 message packet" );
      AsnFree( pAsn );
      return DE_ENCODE_MESSAGE_PACKET_F AILURE;
}
Sorry, but I copied the wrong version to the question earlier. Â The original C function uses a vector iterator as a parameter and I can't use DllImport in C# to call it because C# doesn't have a vector type. Â I tried modifiying the original C code's function to accept only the structure of CUserContextData passed in as the parameter that contains the data to be encoded. Â I was able to compile it successfully in VS C++ compiler as a un-managed code. Â However, when i call this from my C# application, I would get a "memory corruption error". Â I'm able to step through this fuction and I don't know if that is becuase it's a DllImport function. Â Can you see what's wrong in my code here? Â Thank you very much.
Here is how I declare this in C#:
    public class LibWrap
    {
       [DllImport("UnityDecodeAsn
      public static extern DE_ERRORS EncodeAsnUser(ref Blob blob,
         [In, Out]CUserContextData m);
    }
Here is my structure marshaling
    [StructLayout(LayoutKind.S
    public class CUserContextData
    {
      public int bWinLogOn = 0;
      public int bUnifiedID = 0;
      [MarshalAs(UnmanagedType.B
      public String shell = null;
      [MarshalAs(UnmanagedType.B
      public String homeDir = null;
      [MarshalAs(UnmanagedType.B
      public String primaryGroupSID = null;
      [MarshalAs(UnmanagedType.B
      public String LoginName = null;
      public int symarkUID = 0; //Unified User ID
      public int IID = 0;    //Independant ID
      public int Revision = 0;
    }
Here is the c code:
extern "C" DE_ERRORS __declspec(dllexport)Encod
{
      _bstr_t temp;
      AsnData* pAsn;
      std::wstring wsUID;
      int REVISION=0;
      const char* EMPTY_STRING;
      EMPTY_STRING = "";
      if ( ( pAsn = AsnAlloc( NULL ) ) == NULL )
      {
           DbgLog( DL_ERROR, "Unable to allocate ASN.1 buffer" );
           return DE_MEMORY_ALLOCATION_FAILU
      }
      if ( !AsnPushTag(pAsn, (ASN_TAG$APPLICATION_START
           goto failed;
      if (!AsnWriteInteger( pAsn, REVISION))
           goto failed;     Â
      if(userDataIter.bUnifiedID
      {
           if (!AsnWriteInteger( pAsn, True) )
                 goto failed;
      }
      else  //use indep ID
      {
           if (!AsnWriteInteger( pAsn, False) )
                 goto failed;     Â
      }
      if(userDataIter.IID == NULL)
      {
           if ( !AsnWriteGeneralString( pAsn, EMPTY_STRING))
                 goto failed;
      }
      else
      {
           _bstr_t bstrtUID = L"0"; //initilize the bstr
                 _itow(userDataIter.IID, bstrtUID, 10);
           if ( !AsnWriteGeneralString( pAsn, bstrtUID.operator const char *()))
                 goto failed;     Â
      }
     Â
      //WinUser name
      if(userDataIter.bWinLogOn)
      {
           if (!AsnWriteInteger( pAsn, True) )
                 goto failed;
      }
      else//use indep User Name
      {
           if (!AsnWriteInteger( pAsn, False) )
                 goto failed;           Â
      }
      //Geco
      if ( !AsnWriteGeneralString( pAsn, EMPTY_STRING))
           goto failed;
      //Shell
      temp = userDataIter.shell;
      if ( !AsnWriteGeneralString( pAsn, temp.operator const char *()))
           goto failed;     Â
      //Home Driectory
      temp = userDataIter.homeDir;
      if ( !AsnWriteGeneralString( pAsn, temp.operator const char *()))
           goto failed;     Â
      //Primary Group SID
      temp = userDataIter.primaryGroupS
      if (!AsnWriteGeneralString( pAsn, temp.operator const char *()))
           goto failed;     Â
      if ( !AsnPopTag( pAsn ) )
           goto failed;
      *ppBlob = AsnExtractData( pAsn );
      AsnFree( pAsn );
      return DE_SUCCESS;
     Â
failed:
      DbgLog(DL_ERROR, "Failed to encode ASN.1 message packet" );
      AsnFree( pAsn );
      return DE_ENCODE_MESSAGE_PACKET_F
}
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
If you are using a vector, I'd rather pass the vector index as an unsigned integer than marshalling an iterator, since that is way less error-prone. E.g.
extern "C" DE_ERRORS __declspec(dllexport)Encod eAsnUser(B lob** ppBlob, unsigned int userDataIndex)
{
   _bstr_t temp;
   AsnData* pAsn;
   std::wstring wsUID;
   int REVISION=0;
   const char* EMPTY_STRING;
   EMPTY_STRING = "";
   if ( ( pAsn = AsnAlloc( NULL ) ) == NULL )
   {
      DbgLog( DL_ERROR, "Unable to allocate ASN.1 buffer" );
      return DE_MEMORY_ALLOCATION_FAILU RE;
   }
   if ( !AsnPushTag(pAsn, (ASN_TAG$APPLICATION_START ) ) )      Â
      goto failed;
   if (!AsnWriteInteger( pAsn, REVISION))
      goto failed;   Â
   if(userDataVector[userData Index].bUn ifiedID)// use UID
   {
      if (!AsnWriteInteger( pAsn, True) )
         goto failed;
   }
   else  //use indep ID
   {
      if (!AsnWriteInteger( pAsn, False) )
         goto failed;   Â
   }
   if(userDataVector[userData Index].IID == NULL)
   {
      if ( !AsnWriteGeneralString( pAsn, EMPTY_STRING))
         goto failed;
   }
   else
   {
      _bstr_t bstrtUID = L"0"; //initilize the bstr
         _itow(userDataVector[userD ataIndex]. IID, bstrtUID, 10);
      if ( !AsnWriteGeneralString( pAsn, (char*)bstrtUID))
         goto failed;   Â
   }
  Â
   //WinUser name
   if(userDataVector[userData Index].bWi nLogOn)
   {
      if (!AsnWriteInteger( pAsn, True) )
         goto failed;
   }
   else//use indep User Name
   {
      if (!AsnWriteInteger( pAsn, False) )
         goto failed;      Â
   }
   //Geco
   if ( !AsnWriteGeneralString( pAsn, EMPTY_STRING))
      goto failed;
   //Shell
   temp = userDataVector[userDataInd ex].shell;
   if ( !AsnWriteGeneralString( pAsn, (char*)temp))
      goto failed;   Â
   //Home Driectory
   temp = userDataVector[userDataInd ex].homeDi r;
   if ( !AsnWriteGeneralString( pAsn, (char*)temp))
      goto failed;   Â
   //Primary Group SID
   temp = userDataVector[userDataInd ex].primar yGroupSID;
   if (!AsnWriteGeneralString( pAsn, (char*)temp))
      goto failed;   Â
   if ( !AsnPopTag( pAsn ) )
      goto failed;
   *ppBlob = AsnExtractData( pAsn );
   AsnFree( pAsn );
   return DE_SUCCESS;
  Â
failed:
   DbgLog(DL_ERROR, "Failed to encode ASN.1 message packet" );
   AsnFree( pAsn );
   return DE_ENCODE_MESSAGE_PACKET_F AILURE;
}
And, please, get rid of calling '.operator const char *()' explicitly ;o)
extern "C" DE_ERRORS __declspec(dllexport)Encod
{
   _bstr_t temp;
   AsnData* pAsn;
   std::wstring wsUID;
   int REVISION=0;
   const char* EMPTY_STRING;
   EMPTY_STRING = "";
   if ( ( pAsn = AsnAlloc( NULL ) ) == NULL )
   {
      DbgLog( DL_ERROR, "Unable to allocate ASN.1 buffer" );
      return DE_MEMORY_ALLOCATION_FAILU
   }
   if ( !AsnPushTag(pAsn, (ASN_TAG$APPLICATION_START
      goto failed;
   if (!AsnWriteInteger( pAsn, REVISION))
      goto failed;   Â
   if(userDataVector[userData
   {
      if (!AsnWriteInteger( pAsn, True) )
         goto failed;
   }
   else  //use indep ID
   {
      if (!AsnWriteInteger( pAsn, False) )
         goto failed;   Â
   }
   if(userDataVector[userData
   {
      if ( !AsnWriteGeneralString( pAsn, EMPTY_STRING))
         goto failed;
   }
   else
   {
      _bstr_t bstrtUID = L"0"; //initilize the bstr
         _itow(userDataVector[userD
      if ( !AsnWriteGeneralString( pAsn, (char*)bstrtUID))
         goto failed;   Â
   }
  Â
   //WinUser name
   if(userDataVector[userData
   {
      if (!AsnWriteInteger( pAsn, True) )
         goto failed;
   }
   else//use indep User Name
   {
      if (!AsnWriteInteger( pAsn, False) )
         goto failed;      Â
   }
   //Geco
   if ( !AsnWriteGeneralString( pAsn, EMPTY_STRING))
      goto failed;
   //Shell
   temp = userDataVector[userDataInd
   if ( !AsnWriteGeneralString( pAsn, (char*)temp))
      goto failed;   Â
   //Home Driectory
   temp = userDataVector[userDataInd
   if ( !AsnWriteGeneralString( pAsn, (char*)temp))
      goto failed;   Â
   //Primary Group SID
   temp = userDataVector[userDataInd
   if (!AsnWriteGeneralString( pAsn, (char*)temp))
      goto failed;   Â
   if ( !AsnPopTag( pAsn ) )
      goto failed;
   *ppBlob = AsnExtractData( pAsn );
   AsnFree( pAsn );
   return DE_SUCCESS;
  Â
failed:
   DbgLog(DL_ERROR, "Failed to encode ASN.1 message packet" );
   AsnFree( pAsn );
   return DE_ENCODE_MESSAGE_PACKET_F
}
And, please, get rid of calling '.operator const char *()' explicitly ;o)
ASKER
I changed to the pointer and it seems to be working but I can't be sure until I fix the error that I'm getting in the next line of code in C# where I'm adding the meetingBlg to the meeting class of AD. Â I need to figure out how to convert the return pData to a byte array to do that. Â But for now the function is retuning OK. Â Thank you for all the input. Â
ikr, as I said that I don't want to use vector or iterator becuase C# doesn't have that and I just have a user to encode at a time so there is no array. Â Thanks and sorry for if I confuse you.
ikr, as I said that I don't want to use vector or iterator becuase C# doesn't have that and I just have a user to encode at a time so there is no array. Â Thanks and sorry for if I confuse you.
ASKER
So, this is what I have so far. Â Thanks.
    public class CUserContextData
    {
      public int bWinLogOn = 0;
      public int bUnifiedID = 0;
      [MarshalAs(UnmanagedType.B yValTStr, SizeConst = MAX_ADSPATH_CHARS)]
      public String shell = null;
      [MarshalAs(UnmanagedType.B yValTStr, SizeConst = MAX_ADSPATH_CHARS)]
      public String homeDir = null;
      [MarshalAs(UnmanagedType.B yValTStr, SizeConst = MAX_ADSPATH_CHARS)]
      public String primaryGroupSID = null;
      [MarshalAs(UnmanagedType.B yValTStr, SizeConst = MAX_ADSPATH_CHARS)]
      public String LoginName = null;
      public int symarkUID = 0; //Unified User ID
      public int IID = 0;    //Independant ID
      public int Revision = 0;
    }
CUnityDS.CUserContextData userContextData = new CUnityDS.CUserContextData( );
//assign data
CUnityDS.DE_ERRORS errcode = CUnityDS.DE_ERRORS.DE_MEMO RY_ALLOCAT ION_FAILUR E;
errcode = CUnityDS.LibWrap.EncodeAsn User(ref blob, userContextData);
deNewContextObject.Propert ies["meeti ngBlob"].A dd((object )blob.pDat a);
extern "C" DE_ERRORS __declspec(dllexport)Encod eAsnUser(B lob** ppBlob, CUserContextData *userDataIter)
{
      _bstr_t temp;
      AsnData* pAsn;
      std::wstring wsUID;
      int REVISION=0;
      const char* EMPTY_STRING;
      EMPTY_STRING = "";
      if ( ( pAsn = AsnAlloc( NULL ) ) == NULL )
      {
           DbgLog( DL_ERROR, "Unable to allocate ASN.1 buffer" );
           return DE_MEMORY_ALLOCATION_FAILU RE;
      }
      if ( !AsnPushTag(pAsn, (ASN_TAG$APPLICATION_START ) ) )           Â
           goto failed;
      if (!AsnWriteInteger( pAsn, REVISION))
           goto failed;     Â
      if(userDataIter->bUnifiedI D)//use UID
      {
           if (!AsnWriteInteger( pAsn, True) )
                 goto failed;
      }
      else  //use indep ID
      {
           if (!AsnWriteInteger( pAsn, False) )
                 goto failed;     Â
      }
      if(userDataIter->IID == NULL)
      {
           if ( !AsnWriteGeneralString( pAsn, EMPTY_STRING))
                 goto failed;
      }
      else
      {
           _bstr_t bstrtUID = L"0"; //initilize the bstr
                 _itow(userDataIter->IID, bstrtUID, 10);
           if ( !AsnWriteGeneralString( pAsn, (char*)bstrtUID))
                 goto failed;     Â
      }
     Â
      //WinUser name
      if(userDataIter->bWinLogOn )
      {
           if (!AsnWriteInteger( pAsn, True) )
                 goto failed;
      }
      else//use indep User Name
      {
           if (!AsnWriteInteger( pAsn, False) )
                 goto failed;           Â
      }
      //Geco
      if ( !AsnWriteGeneralString( pAsn, EMPTY_STRING))
           goto failed;
      //Shell
      temp = userDataIter->shell;
      if ( !AsnWriteGeneralString( pAsn, (char*)temp))
           goto failed;     Â
      //Home Driectory
      temp = userDataIter->homeDir;
      if ( !AsnWriteGeneralString( pAsn, (char*)temp))
           goto failed;     Â
      //Primary Group SID
      temp = userDataIter->primaryGroup SID;
      if (!AsnWriteGeneralString( pAsn, (char*)temp))
           goto failed;     Â
      if ( !AsnPopTag( pAsn ) )
           goto failed;
      *ppBlob = AsnExtractData( pAsn );
      AsnFree( pAsn );
      return DE_SUCCESS;
     Â
failed:
      DbgLog(DL_ERROR, "Failed to encode ASN.1 message packet" );
      AsnFree( pAsn );
      return DE_ENCODE_MESSAGE_PACKET_F AILURE;
}
    public class CUserContextData
    {
      public int bWinLogOn = 0;
      public int bUnifiedID = 0;
      [MarshalAs(UnmanagedType.B
      public String shell = null;
      [MarshalAs(UnmanagedType.B
      public String homeDir = null;
      [MarshalAs(UnmanagedType.B
      public String primaryGroupSID = null;
      [MarshalAs(UnmanagedType.B
      public String LoginName = null;
      public int symarkUID = 0; //Unified User ID
      public int IID = 0;    //Independant ID
      public int Revision = 0;
    }
CUnityDS.CUserContextData userContextData = new CUnityDS.CUserContextData(
//assign data
CUnityDS.DE_ERRORS errcode = CUnityDS.DE_ERRORS.DE_MEMO
errcode = CUnityDS.LibWrap.EncodeAsn
deNewContextObject.Propert
extern "C" DE_ERRORS __declspec(dllexport)Encod
{
      _bstr_t temp;
      AsnData* pAsn;
      std::wstring wsUID;
      int REVISION=0;
      const char* EMPTY_STRING;
      EMPTY_STRING = "";
      if ( ( pAsn = AsnAlloc( NULL ) ) == NULL )
      {
           DbgLog( DL_ERROR, "Unable to allocate ASN.1 buffer" );
           return DE_MEMORY_ALLOCATION_FAILU
      }
      if ( !AsnPushTag(pAsn, (ASN_TAG$APPLICATION_START
           goto failed;
      if (!AsnWriteInteger( pAsn, REVISION))
           goto failed;     Â
      if(userDataIter->bUnifiedI
      {
           if (!AsnWriteInteger( pAsn, True) )
                 goto failed;
      }
      else  //use indep ID
      {
           if (!AsnWriteInteger( pAsn, False) )
                 goto failed;     Â
      }
      if(userDataIter->IID == NULL)
      {
           if ( !AsnWriteGeneralString( pAsn, EMPTY_STRING))
                 goto failed;
      }
      else
      {
           _bstr_t bstrtUID = L"0"; //initilize the bstr
                 _itow(userDataIter->IID, bstrtUID, 10);
           if ( !AsnWriteGeneralString( pAsn, (char*)bstrtUID))
                 goto failed;     Â
      }
     Â
      //WinUser name
      if(userDataIter->bWinLogOn
      {
           if (!AsnWriteInteger( pAsn, True) )
                 goto failed;
      }
      else//use indep User Name
      {
           if (!AsnWriteInteger( pAsn, False) )
                 goto failed;           Â
      }
      //Geco
      if ( !AsnWriteGeneralString( pAsn, EMPTY_STRING))
           goto failed;
      //Shell
      temp = userDataIter->shell;
      if ( !AsnWriteGeneralString( pAsn, (char*)temp))
           goto failed;     Â
      //Home Driectory
      temp = userDataIter->homeDir;
      if ( !AsnWriteGeneralString( pAsn, (char*)temp))
           goto failed;     Â
      //Primary Group SID
      temp = userDataIter->primaryGroup
      if (!AsnWriteGeneralString( pAsn, (char*)temp))
           goto failed;     Â
      if ( !AsnPopTag( pAsn ) )
           goto failed;
      *ppBlob = AsnExtractData( pAsn );
      AsnFree( pAsn );
      return DE_SUCCESS;
     Â
failed:
      DbgLog(DL_ERROR, "Failed to encode ASN.1 message packet" );
      AsnFree( pAsn );
      return DE_ENCODE_MESSAGE_PACKET_F
}
>>I changed to the pointer
Why not using an integer as the index into the vector? That's easy to handle on the C# side and you just have to increment it...
Why not using an integer as the index into the vector? That's easy to handle on the C# side and you just have to increment it...
ASKER
Hi ikr,
In C# I'm passing a user at a time to decode. Â I don't have an array of users. Â I thought I mentioned in the quedstion that I would like to use the function to convert 1 user at a time. Â I'm sorry if I wasn't clear. Â Thank you for your help. Â
Can you help me with another problem  ID: 22637643?  I just posted this question and it's kind of the continuation of this question.  Thank you.
In C# I'm passing a user at a time to decode. Â I don't have an array of users. Â I thought I mentioned in the quedstion that I would like to use the function to convert 1 user at a time. Â I'm sorry if I wasn't clear. Â Thank you for your help. Â
Can you help me with another problem  ID: 22637643?  I just posted this question and it's kind of the continuation of this question.  Thank you.
   //Home Driectory
   temp = userDataIter->homeDir.c_st
   if ( !AsnWriteGeneralString( pAsn,(char*) temp))
      goto failed;   Â
   //Primary Group SID
   temp = userDataIter->primaryGroup
   if (!AsnWriteGeneralString( pAsn, (char*)temp))
      goto failed;   Â
And, what's even more important - how are you managing that iterator you pass in?