By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,515 Members | 1,848 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,515 IT Pros & Developers. It's quick & easy.

Using C++ dll with C#

P: 3
Hi there every1, i've been stuck on a problem for longer then any1 could of expected...

My problem is that i have a method that i have to call in C#, that comes from C++ DLL, easy enough to call a method but what i cant get is that, i have to send a pointer to a struct as a parameter.

My second problem is that a few of the data type's have to change.

The code looks like this.

<<Struct>>
typedef struct{
unsigned ID;
unsigned short reserved1;
unsigned short adminLevel;
unsigned short securityLevel;
unsigned short statusMask; // internally used by BIOSTATION
unsigned accessGroupMask;
char name[32];
char department[32];
char password[16];
unsigned short numOfFinger;
unsigned short duressMask;
unsigned short checksum[5];
} BSUserHdr;
<<Here is the example used in C++>>
BSUserHdr userHeader;
userHeader.ID = 1; // 0 cannot be assigned as a user ID.
userHeader.adminLevel = 240;
userHeader.securityLevel = 260;
userHeader.accessGroupMask = 0; // a member of Group 1 and Group 2;
strcpy( userHeader.name, “John” );
strcpy( userHeader.departments, “R&D” );
strcpy( userHeader.password, NULL ); // no password is enrolled. Password

// should be longer than 4 bytes.
userHeader.numOfFinger = 2;
unsigned char* templateBuf = (unsigned char*)malloc( userHeader.numOfFinger * 2 * BS_TEMPLATE_SIZE );
// fill template data
userHeader.duressMask = 0; // no duress finger
for( int i = 0; i < userHeader.numOfFinger * 2; i++ )
{
if( i % 2 == 0 )
{
userHeader.checksum[i/2] = 0;
}
unsigned char* templateData = templateBuf + i * BS_TEMPLATE_SIZE;
for( int j = 0; j < BS_TEMPLATE_SIZE; j++ )
{
userHeader.checksum[i/2] += templateData[j];
}
}
BS_RET_CODE result = BS_EnrollUser( handle, &userHeader, templateBuf );
<<this what i have attempted so far in C#>>
public int EnrollUser(int id, String nme, String pswd, String dept, String adminLevel, String securityLevel)
{
int success = 0;
short BS_TEMPLATE_SIZE = 384;




try
{
BSUserHdr userHeader = new BSUserHdr();
userHeader.checksum = new short[5];
//userHeader.ID = 2;
userHeader.ID = id;
userHeader.numOfFinger = 1;

//password
int pwdSize = 16;
byte[] pwd = new byte[pwdSize];
//String password = "3456";
String password = pswd;
for (int index = 0; index < password.Length; index++)
{
pwd[index] = Convert.ToByte(password[(index)]);
}
userHeader.password = pwd;

//name
int nameSize = 32;
byte[] name = new byte[nameSize];
//String Name = "Regan";
String Name = nme;
for (int index = 0; index < Name.Length; index++)
{
name[index] = Convert.ToByte(Name[(index)]);
}
userHeader.name = name;

//Department
int depSize = 32;
byte[] dep = new byte[depSize];
//String department = "Development";
String department = dept;
for (int index = 0; index < department.Length; index++)
{
dep[index] = Convert.ToByte(department[(index)]);
}
userHeader.department = dep;

//Admin Level
if (adminLevel == "BS_USER_ADMIN")
{
userHeader.adminLevel = 240;
}
else
{
if (adminLevel == "BS_USER_NORMAL")
{
userHeader.adminLevel = 241;
}
else
{
throw new Exception("Incorrect user type");
}
}

//Access Group Mask
//userHeader.accessGroupMask = Convert.ToInt32(0xffffffff);
userHeader.accessGroupMask = Convert.ToInt32(0);

//security Level
userHeader.securityLevel = 260;
switch(securityLevel)
{
case "BS_USER_SECURITY_DEFAULT": userHeader.securityLevel = 260;
break;
case "BS_USER_SECURITY_LOWER": userHeader.securityLevel = 261;
break;
case "BS_USER_SECURITY_LOW": userHeader.securityLevel = 262;
break;
case "BS_USER_SECURITY_NORMAL": userHeader.securityLevel = 263;
break;
case "BS_USER_SECURITY_HIGH": userHeader.securityLevel = 264;
break;
case "BS_USER_SECURITY_HIGHER": userHeader.securityLevel = 265;
break;
default:
throw new Exception("Incorrect Security Level");
break;
}

//DuressMask
//userHeader.duressMask = Convert.ToInt32(0xff);
userHeader.duressMask = 0;

//Fingers
byte[] templateBuffer = new byte[userHeader.numOfFinger * BS_TEMPLATE_SIZE * 2];

for (int i = 0; i < (userHeader.numOfFinger * 2); i++)
{
byte[] finger = new byte[BS_TEMPLATE_SIZE];


BS_Disable(handle, 30);
success = ScanTemplate(ref finger);
BS_Enable(handle);

int cursorPosition = i * BS_TEMPLATE_SIZE;
int index = 0;
for (int x = cursorPosition; x < (cursorPosition + BS_TEMPLATE_SIZE); x++)
{
templateBuffer[x] = finger[index];
index++;
}
int k = 0;
int cursorPosition1 = i * BS_TEMPLATE_SIZE;
for (int j = cursorPosition1; j < BS_TEMPLATE_SIZE; j++)
{
userHeader.checksum[i / 2] += Convert.ToInt16(finger[k]);
k++;
}

}

success = BS_EnrollUser(handle, ref userHeader, templateBuffer);

}
catch (Exception err)
{
throw new Exception(err.Message);
}

return success;
}
<<>>
It looks like a mess....

Any assistance will be greatly appreciated

Thanx every1.
May 28 '07 #1
Share this Question
Share on Google+
2 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
There's something about interoperability here.

Check out the C# DLL class that extends Object.

I assume the C++ dll works in C++. Yes?
May 28 '07 #2

P: 3
There's something about interoperability here.

Check out the C# DLL class that extends Object.

I assume the C++ dll works in C++. Yes?
Yea the c++ DLL works. The C# dll is also working because im using other methods from the dll and they work. it is just this method, i think im doing some thing wrong with the convertion of data types.

the code runs but when i call the method from the dll i get a error saying
that there is a memory problem, but i do not see where they require pointers and memory allocation.
May 29 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.