tags:

views:

235

answers:

4

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.

+1  A: 

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 Desigsn, 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).

Ira Baxter
sounds interesting :)
J Angwenyi
How maintenable and idiomatic was the resulting code? I've spend some time in a machine translated code base and I'd have preferred to use the original language. If "real programmers are able to write FORTRAN in any language", automatic translation from FORTRAN for sure succeed in meeting that "goal" very well.
AProgrammer
Its easy to build a bad translator: simply map individual langauge operators directly to the target language without regard to context. While you have to map langauge constructs, you can do so taking in account context (DMS is able to gather facts from across the application and use them to control what is generated at each point), and you can post-optimize the translated result. Both techniques make the code a lot more maintainable.
Ira Baxter
A: 

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);
Mattias S
@Mattias s - the full code listing is at this site:http://msdn.microsoft.com/en-us/library/aa363765(VS.85).aspx
J Angwenyi
A: 

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.

Alan
A: 

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.

Romain Verdier