G
Guest
Hi, I'm using vs2005, .net 2, C# for Windows application. I use DllImport so
I can call up a function written in C++ as unmanaged code and compiled as a
dll us vs2005. My application is able to call the function, EncodeAsnUser.
And it's returning OK but when I display the decoded data in another part of
my application it shows no data has been decoded, all fiedls are either null
or blanks. For some reason, I am not able to step through this function's
code. However, I can see the userContextData does hold all the data that I
assigned and that it's passing.
I think the pbroblems is with my declaration of the parameters(blob and the
userdata structure) .
1. Am I passing the userContextData as a pointer to the function?
2. Am I allocating memory correctly for the Blob?
3. Am I passing the Blob** correctly?
Can someone see what I need to correct here to make it work? Thank you.
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public class CUserContextData
{
public int bWinLogOn = 0;
public int bUnifiedID = 0;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst =
MAX_ADSPATH_CHARS)]
public String shell = null;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst =
MAX_ADSPATH_CHARS)]
public String homeDir = null;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst =
MAX_ADSPATH_CHARS)]
public String primaryGroupSID = null;
[MarshalAs(UnmanagedType.ByValTStr, 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;
}
public struct Blob
{
public IntPtr pData;
public int nLength;
public int nSize;
}
public class LibWrap
{
[DllImport("UnityDecodeAsnUser.dll", CharSet = CharSet.Unicode)]
public static extern DE_ERRORS EncodeAsnUser(ref Blob blob,
[In, Out]CUserContextData m);
}
// create an instance of the Blob structure
CUnityDS.Blob blob = new CUnityDS.Blob();
//Encode the user data into a blob
int nBytes = Marshal.SizeOf(typeof(CUnityDS.Blob));
IntPtr ptr = Marshal.AllocHGlobal(nBytes);
// copy and pin the structure to that location
Marshal.StructureToPtr(blob, ptr, true);
// Pass it by reference
// OK, now it's time to "reconsitute" the structure
blob = (CUnityDS.Blob)Marshal.PtrToStructure(ptr,
typeof(CUnityDS.Blob));
CUnityDS.DE_ERRORS errcode =
CUnityDS.DE_ERRORS.DE_MEMORY_ALLOCATION_FAILURE;
errcode = CUnityDS.LibWrap.EncodeAsnUser(ref blob,
userContextData);
//Convert from IntPtr data to byte[] which is needed
for adding as meetingBlob
int size = Marshal.SizeOf(blob);
byte[] meetingBlob = new byte[size];
System.Runtime.InteropServices.Marshal.Copy(blob.pData, meetingBlob, 0,
size);
//add the blob
deNewContextObject.Properties["meetingBlob"].Add((object)meetingBlob);
deNewContextObject.CommitChanges();
//This is the C++ function that I'm calling
extern "C" DE_ERRORS __declspec(dllexport)EncodeAsnUser(Blob** ppBlob,
CUserContextData *userData)
{
_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_FAILURE;
}
if ( !AsnPushTag(pAsn, (ASN_TAG$APPLICATION_START) ) )
goto failed;
if (!AsnWriteInteger( pAsn, REVISION))
goto failed;
if(userData->bUnifiedID)//use UID
{
if (!AsnWriteInteger( pAsn, True) )
goto failed;
}
else //use indep ID
{
if (!AsnWriteInteger( pAsn, False) )
goto failed;
}
if(userData->IID == NULL)
{
if ( !AsnWriteGeneralString( pAsn, EMPTY_STRING))
goto failed;
}
else
{
_bstr_t bstrtUID = L"0"; //initilize the bstr
_itow(userData->IID, bstrtUID, 10);
if ( !AsnWriteGeneralString( pAsn, (char*)bstrtUID))
goto failed;
}
//WinUser name
if(userData->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 = userData->shell;
if ( !AsnWriteGeneralString( pAsn, (char*)temp))
goto failed;
//Home Driectory
temp = userData->homeDir;
if ( !AsnWriteGeneralString( pAsn, (char*)temp))
goto failed;
//Primary Group SID
temp = userData->primaryGroupSID;
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_FAILURE;
I can call up a function written in C++ as unmanaged code and compiled as a
dll us vs2005. My application is able to call the function, EncodeAsnUser.
And it's returning OK but when I display the decoded data in another part of
my application it shows no data has been decoded, all fiedls are either null
or blanks. For some reason, I am not able to step through this function's
code. However, I can see the userContextData does hold all the data that I
assigned and that it's passing.
I think the pbroblems is with my declaration of the parameters(blob and the
userdata structure) .
1. Am I passing the userContextData as a pointer to the function?
2. Am I allocating memory correctly for the Blob?
3. Am I passing the Blob** correctly?
Can someone see what I need to correct here to make it work? Thank you.
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public class CUserContextData
{
public int bWinLogOn = 0;
public int bUnifiedID = 0;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst =
MAX_ADSPATH_CHARS)]
public String shell = null;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst =
MAX_ADSPATH_CHARS)]
public String homeDir = null;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst =
MAX_ADSPATH_CHARS)]
public String primaryGroupSID = null;
[MarshalAs(UnmanagedType.ByValTStr, 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;
}
public struct Blob
{
public IntPtr pData;
public int nLength;
public int nSize;
}
public class LibWrap
{
[DllImport("UnityDecodeAsnUser.dll", CharSet = CharSet.Unicode)]
public static extern DE_ERRORS EncodeAsnUser(ref Blob blob,
[In, Out]CUserContextData m);
}
// create an instance of the Blob structure
CUnityDS.Blob blob = new CUnityDS.Blob();
//Encode the user data into a blob
int nBytes = Marshal.SizeOf(typeof(CUnityDS.Blob));
IntPtr ptr = Marshal.AllocHGlobal(nBytes);
// copy and pin the structure to that location
Marshal.StructureToPtr(blob, ptr, true);
// Pass it by reference
// OK, now it's time to "reconsitute" the structure
blob = (CUnityDS.Blob)Marshal.PtrToStructure(ptr,
typeof(CUnityDS.Blob));
CUnityDS.DE_ERRORS errcode =
CUnityDS.DE_ERRORS.DE_MEMORY_ALLOCATION_FAILURE;
errcode = CUnityDS.LibWrap.EncodeAsnUser(ref blob,
userContextData);
//Convert from IntPtr data to byte[] which is needed
for adding as meetingBlob
int size = Marshal.SizeOf(blob);
byte[] meetingBlob = new byte[size];
System.Runtime.InteropServices.Marshal.Copy(blob.pData, meetingBlob, 0,
size);
//add the blob
deNewContextObject.Properties["meetingBlob"].Add((object)meetingBlob);
deNewContextObject.CommitChanges();
//This is the C++ function that I'm calling
extern "C" DE_ERRORS __declspec(dllexport)EncodeAsnUser(Blob** ppBlob,
CUserContextData *userData)
{
_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_FAILURE;
}
if ( !AsnPushTag(pAsn, (ASN_TAG$APPLICATION_START) ) )
goto failed;
if (!AsnWriteInteger( pAsn, REVISION))
goto failed;
if(userData->bUnifiedID)//use UID
{
if (!AsnWriteInteger( pAsn, True) )
goto failed;
}
else //use indep ID
{
if (!AsnWriteInteger( pAsn, False) )
goto failed;
}
if(userData->IID == NULL)
{
if ( !AsnWriteGeneralString( pAsn, EMPTY_STRING))
goto failed;
}
else
{
_bstr_t bstrtUID = L"0"; //initilize the bstr
_itow(userData->IID, bstrtUID, 10);
if ( !AsnWriteGeneralString( pAsn, (char*)bstrtUID))
goto failed;
}
//WinUser name
if(userData->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 = userData->shell;
if ( !AsnWriteGeneralString( pAsn, (char*)temp))
goto failed;
//Home Driectory
temp = userData->homeDir;
if ( !AsnWriteGeneralString( pAsn, (char*)temp))
goto failed;
//Primary Group SID
temp = userData->primaryGroupSID;
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_FAILURE;