Help With C# Wrapper

Announcements and discussion for PInvoke

Moderators: Luke Jefferson, Robert

Help With C# Wrapper

Postby Ric Liasa » Mon Aug 27, 2012 5:58 pm

Hi

I have a DLL that is written in C/C++ (Unmanaged code) and I want to create a C# wrapper for it, but I'm having problems with some functions.

The name of the DLL is 'sgenc32.dll', I not have the source code but I have the exposed functions into it.

the functions exposed that i want to call are:
Code: Select all
int SearchCurpsG(char *sourceFile, int documentType, SAFEARRAY **arr_CURPS);

int SignG(char *sourceFile, int documentType, SAFEARRAY **arr_CURPS);


I'm not sure, but i think that my problem is with the SAFEARRAY because the functions are not doing that they need to do.

The SAFEARRAY is an array of structs, and this is the struct that i need:
C/C++ Code:
Code: Select all
typedef struct struct_curps
{
    unsigned short    curp[19],
                             keyFile[256],
                             cerFile[256],
                             password[256];
} struct_curps;

I think that must be char* instead \"unsigned short\" but this is how it is in
the user's guide

I'm doing the wrapper like this:
C# Wrapper
Code: Select all
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct struct_curps
{
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 19)]
    public string curp;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
    public string keyFile;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
    public string cerFile;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
    public string password;
}

[DllImport(\"sgenc32.dll\", EntryPoint = \"_SearchCurpsG@12\")]
static extern int SearchCurpsG([MarshalAs(UnmanagedType.LPStr)] String sourceFile,
                                             int documentType,
                                             ref struct_curps[] SAFEARRAY);

[DllImport(\"sgenc32.dll\", EntryPoint = \"_SignG@12\")]
static extern int SignG([MarshalAs(UnmanagedType.LPStr)] String sourceFile,
                                 int documentType,
                                 ref struct_curps[] SAFEARRAY);

int result;
struct_curps[] safearray = struct_curps[1];
result = SearchCurpsG(@\"C:\\m6002400.220\", 0, safearray);


The function return 0 = OK, but the function must retrieve a data from a file and save it to the field curp into the struct, is an array because the file can have more than one data, unfortunately function is not saving data into the structs and I'm sure that the file has data to retrieve.

After this i need fill the rest of the fields to sign the file
Code: Select all
safearray[0].keyFile = @\"C:\\file.key\";
safearray[0].cerFile = @\"C:\\file.cer\";
safearray[0].password = \"**********\";

result = SignG(\"C:\\m6002400.220\", 0, safearray);

The function return 1 = pathfile

The other functions into the dll are working fine and only this couple of functions with parameter as SAFEARRAY are not working.

This is the sample code in the user's guide
C/C++ Code
Code: Select all
struct struct_curps CURPS[5];
int i[1] = {0};

SAFEARRAY *arreglo;
SAFEARRAYBOUND rgsabound[1];

rgsabound[0].lLbound = 0;
rgsabound[0].cElements = 5;
arreglo = SafeArrayCreate(VT_VARIANT, 1, rgsabound);
if(arreglo == NULL)
   return E_OUTOFMEMORY;

... SearchCurpsG(…,…, &arreglo);

for( i[0] = 0 ; i[0] < 5; i[0]++ )
   SafeArrayGetElement(*arr_CURPS, i, &CURPS[i[0]])

strcpy(CURPS[0].keyFile, fileNameKEY);
strcpy(CURPS[0].cerFile, fileNameCER);
strcpy(CURPS[0].password, ThePassword);
.
.
.
.
strcpy(CURPS[4].keyFile, fileNameKEY);
strcpy(CURPS[4].cerFile, fileNameCER);
strcpy(CURPS[4].password, ThePassword);

for (i[0] = 0; i[0] < 5; i[0]++ )
   SafeArrayPutElement(*arr_CURPS, i, &CURPS[i[0]]);

... SignG(…,…, &arreglo);


Anybody knows what's the right way to pass SAFEARRAY of structs to unmanaged code?
Ric Liasa
 
Posts: 1
Joined: Mon Aug 27, 2012 4:32 pm

Postby Brian Donahue » Wed Aug 29, 2012 9:07 am

I'm sorry you have not got an answer to this. Unfortunately, Red Gate will only respond to questions about the PInvoke wiki and do not offer consultation services for programmers. I would give you an answer if I had one, but I am not this skilled at programming myself.

You may want to pose this question at the MSDN developer forums.

Thanks for your interest.
Brian Donahue
 
Posts: 6670
Joined: Mon Aug 23, 2004 10:48 am


Return to PInvoke.Net

Who is online

Users browsing this forum: No registered users and 0 guests