Problem with calling unmanaged function from VC++

  • Thread starter Thread starter darkhonor
  • Start date Start date
D

darkhonor

I'm very new to Windows development, all
of my background is in Linux, so please bear with me. I am working
on a class project and just for fun I'm building a frontend using
VC++ 2003. The project uses the OpenSSL crypto libraries
(specifically the BIGNUM functions). The offending code is:


if (this->rbtnSetKeysize->Checked) {
this->intKeysize = 512;
// Load the Prime Number and calculate the Secret Value
this->statusBar1->Text = "Generating Prime and Secret
values...";
this->bnPrime = BN_generate_prime(NULL,
this->intKeysize, 1, NULL, NULL, NULL, NULL);
this->bnTmp = BN_generate_prime(NULL, this->intKeysize,
1, NULL, NULL, NULL, NULL);
BN_mod(this->bnSecret, this->bnTmp, this->bnPrime,
this->bnctxContext);
this->statusBar1->Text = "Prime and Secret values
generated.";
} else {
this->statusBar1->Text = "Loading Prime Number from
File...";

FileStream *fs = new
FileStream(this->txtInputFile->Text,
FileMode::Open, FileAccess::Read,
FileShare::None);
StreamReader *sr = new StreamReader(fs);
BN_hex2bn(&bnPrime, sr->ReadLine());
fs->Close();

this->intKeysize = BN_num_bits(this->bnPrime);
this->statusBar1->Text = "Generating Secret
Value...";
this->bnTmp = BN_generate_prime(NULL, this->intKeysize,
1, NULL, NULL, NULL, NULL);
BN_mod(this->bnSecret, this->bnTmp, this->bnPrime,
this->bnctxContext);
this->statusBar1->Text = "Secret Value
Generated";
} // End If


I am trying to call the BN_hex2bn function, which takes the address of
a dynamic BIGNUM as a parameter. I have bnPrime declared as a BIGNUM
* variable. However, when the call to the function in the snippet
below goes through the compiler, I get the following error:

error C2664: 'BN_hex2bn': cannot convert parameter 1 from 'BIGNUM
*__gc *' to 'BIGNUM **'

Any ideas? I'm having other problems as well, but I figured I
wouldn't hit you all at once. Thanks for your help.

Alex
 
In VC++ .NET the __gc type is garbage collected. This means that the
run-time is free to move it around in memory anytime
it wants. Thus, you cannot pass a pointer that could be moved into a
non-managed routine. You need to pin the pointer
(locking it's memory location) before passing it to a non-managed function.
The compiler is trying to help you avoid
this problem by making it impossible to cast the pointer. Once you pin it,
you can cast it any way you need (that
makes sense).

void __pin * newptr = bnPrime;

Once newptr goes out of scope (or is set to zero), the __gc object gets
unpinned from memory.

Check out the help for __pin and other .NET isms (__box, etc.).

-- Tom
 
Can you please tell us which type of project did you select (in VC++
2003). Do you intend to target your application to .Net Framework?
Please elaborate.

Regards,
--Rao TRN
 
Back
Top