Red Gate forums :: View topic - Help With C# Wrapper
Return to RSS Feed Available

Search  | Usergroups |  Profile |  Messages |  Log in  Register 
Go to product documentation
PInvoke.Net forum

Help With C# Wrapper

Search in PInvoke.Net forum
Post new topic   Reply to topic
Jump to:  
Author Message
Ric Liasa

Joined: 27 Aug 2012
Posts: 1

PostPosted: Mon Aug 27, 2012 5:58 pm    Post subject: Help With C# Wrapper Reply with quote


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:
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:
typedef struct struct_curps
    unsigned short    curp[19],
} 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
[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
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
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?
Back to top
View user's profile Send private message
Brian Donahue

Joined: 23 Aug 2004
Posts: 6581

PostPosted: Wed Aug 29, 2012 9:07 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic All times are GMT + 1 Hour
Page 1 of 1

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB © 2001, 2005 phpBB Group