简体   繁体   中英

Converting unmanaged C++ code to C#

Anyone with pointers to a tool/utility for converting unmanaged c++ to c#? I have tried the http://www.pinvoke.net/ site but I cant find reference to this API AddUsersToEncryptedFile on this question .

In general this is really hard, because C++ offer different features than C#: templates, friends, zero-terminated strings, unmanaged pointers, COM, etc., not to mention that parsing C++ is a bitch of a job.

To do it, you need a full C++ parser with name and type resolution, a set of ideas about how to convert each construction (problematic or not) into equivalent C# code, a means to encode those ideas into automated translation steps, and a plan for what to do with those parts of the code that don't translate well (typically, "fix by hand").

Using the DMS Software Reengineering Toolkit , which provides all the requisite machinery, my company, Semantic Designs, actually constructed such a tool, but never used it, for a large customer that wanted to move 800K SLOC of C++ into C#. About 2/3 of the way through the project, the customer had some birdcage management reshuffle, and the new managers decided not to proceed to save money (the tool itself was doing fine).

You may find this story interesting :

Disclosing How C#-SQLite Was Ported to .NET

SQLite is a C program, but there are some good high level tips here.

Is all you need a declaration of that API? There's a tool called PInvoke Interop Assistant you can use, but personally I prefer the DIY approach. It's not that hard.

Try the following definitions

struct EFS_CERTIFICATE_BLOB
{
    public int dwCertEncodingType;
    public int cbData;
    public IntPtr pbData;
}

struct ENCRYPTION_CERTIFICATE
{
    public int cbTotalLength;
    public IntPtr pUserSid;
    public IntPtr pCertBlob;
}

struct ENCRYPTION_CERTIFICATE_LIST
{
    public int nUsers;
    public IntPtr pUsers;
}

[DllImport("advapi32.dll", CharSet=CharSet.Unicode)]
static extern uint AddUsersToEncryptedFile(string lpFileName, ref ENCRYPTION_CERTIFICATE_LIST pUsers);

If you're porting C++ code to C#, then you should perhaps consider converting the unmanaged C++ code to C++/CLI. You can then begin to port to C# in a more controlled manor (ie. by one piece at a time - ideally unit tested as you go).

An alternative to converting to C++/CLI would be to wrap your existing unmanaged C++ code using SWIG , however the migration will be made more difficult using this method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM