Hi, I'm a C / C++ noob, but have been a software engineer for 28 years. I just started a new job, and have inherited some unmanaged C code to maintain.
I need to modify this C code to call a C++ managed / CLR DLL that will take care of decrypting data.
I've searched the web, but nothing seems to fit the specific case I have.
I'd post code, but there's nothing to it. In pseudocode it's :
VS 2010 C program :
read file
while not(eof)
call managed DLL decrypt routine to decrypt certain values
read next record
end While
VS 2010 Managed C++ DLL
Get parm
Decrypt parm
Return value
I've tried various things like regasm and so on but got nowhere.
Thanks, I'll give that a try and get back to you in the afternoon my time (Ireland). Having a crazy day today so it will be approx 2 hrs before I get to try it out.
No way it will covert in the time I have available - it's part of a larger C based application, and it's not well written (in my opinion). Also, I'm a C++ noob, so there's no way I've got the skills yet to do a job like that in a short space of time. Trying to compile with the c++ compiler gives a couple of hundred errors.
I guess I need to re-think this - I wanted to keep the C application as is, apart from a call to my external c++ DLL, but it looks like I'll have to get into the C code and accomplish what I need there.
For the moment a trivial XOR encryption will suffice, and over the weekend I'll try to implement libtomcrypt with the application.
Is there an easy to use C based encryption library you would recommend, as an alternative to libtomcrypt?
It's part of a in-house ERP package, used for controlling the packing out of active ingredient for a pharmaceutical plant. Really needs to be replaced with something more modern and maintainable, and more in line with FDA guidelines for such systems.
The guy who usually worked on it got his ass fired on Monday, so I got lumped with it :(
Thanks a million for taking the time to help with this - I'll check out openssl over the weekend.
Good luck with it. BTW - I'm in the UK. I don't have Windows at home, I have OSX and Linux at home, so won't be able to help out over the weekend apart from advice.
#pragma once
// net_shim.h
#ifdef __cplusplus
extern"C" {
#endif
int write_msg(char* pszMsg);
#ifdef __cplusplus
} // extern "C"
#endif
The properties of the .cpp file need to be altered so that the /clr compiler flag is used for this file. Note it means you have to adjust some of the other default flags (e.g. /ZI -> /Zi, for the debug build)
1 2 3 4 5 6 7 8 9 10 11 12 13
// net_shim.cpp (built with /clr compiler flag, so it
// supports managed code)
usingnamespace System;
#include "net_shim.h"
int write_msg(char* pszMsg)
{
String^ msg = gcnew String(pszMsg);
Console::WriteLine(msg);
return 0;
}
Hello, .Net world
Whether this approach is better or not, than swapping to an alternative cryptographic library, depends on the data types you need to pass to the .Net functions (i.e. how much work there is to marshall the data)
This is perfect - now I can encapsulate the code changes in a C++ program and use the System::Security::Cryptography library for my task. Minimum changes will then be required to the original C code, which is exactly what I needed.
Thank you both for your efforts and your time - you have really saved my bacon this week!! I hope I get the opportunity to repay the favour in the future!
Now I have to write a mountain of documentation for the validation team!#
But I only did it to remind myself how to use COM from C (not something I'd do by choice!)
Andy
PS To get the .h and _i.c you've got to open the .tlb file generated by regasm.exe with OLE-COM Object Viewer and export (Save As) the .h, .c, and .idl (This didn't work for me: the idl file ws truncated. So I cut and paste the .idl from the viewer's windows and then ran midl on it:
midl /Oicf sManagedDLL.IDL /h sManagedDLL.h
Then copied the files where I needed them.
PPS This knowledgebase article does seem to be long in the tooth. Even with VC++2008 I had to make tweaks (inc. the inconsistent name of the managed DLL -- sManagedDLL vs ManagedDLL).