Re: Migrating VC++ 6.0 apps to .NET

From: Ronald Laeremans [MSFT] (ronaldl_at_online.microsoft.com)
Date: 08/23/04


Date: Mon, 23 Aug 2004 10:10:04 -0700

You need to pin the arrays and then passed the pinnen version. You do that
by pinning one of the elements (by convention the first one).

See the docs for the __pin keyword.

Ronald Laeremans
Visual C++ team

"Steve" <Steve@discussions.microsoft.com> wrote in message
news:EE74F79C-CA35-487B-850C-50C4007577B4@microsoft.com...
> We are considering the ways to migrate our VC++ 6.0 applications to .NET
> platform.
> It would be nice to rewrite them completely in C#, but due to the time
> constraints
> this option is out of question. We started from rewriting some dll in C#
> in
> Visual
> Studio .NET. Now we face chalenges of interoperation between old VC++ 6.0
> apps and new .NET C# components. To solve this problem we considered the
> following options:
>
> 1) Recompile old VC++ 6.0 app in Visual Studio .NET without /CLR and use
> COM
> introperability to connect this app and new .NET C# components. It works
> more
> or less OK as long as new .NET C# component expose interfaces. But it is
> extremely difficult to access just methods of the class whish is not
> exposed
> as interface in .NET C# component. TBLexp automatically creates interfaces
> for such classes and this is complete mess. One methods somehow become
> property, names are hardly recognizable etc.
>
> So I tried another approach.
>
> 2) Recompile old VC++ 6.0 app in Visual Studio .NET with /CLR and use "It
> just works" approach. In this case I have no troubles to access interfaces
> or
> classes from new .NET C# component. However I very quickly discovered the
> managed/unmanaged code interaction nightmare. The new .NET C# component
> methods of course require managed parameters, and rest of the old VC++
> application code of course expects unmanaged types.
>
> We don't want to convert the old VC++ application code to Managed C++ in
> one
> shoot, so the idea was to keep old application code intact (may be with
> minor
> changies) and just recompile it in Visual Studio .NET to gain easy access
> to
> new .NET C# components.
>
> But because of this managed/unmanaged hell I can't even make simple
> function
> call to new .NET C# components. For example see the following code
> segment:
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> unsigned char cMessage __gc[] = new unsigned char __gc[256];
> unsigned char cResult __gc[] = new unsigned char __gc[256];
> unsigned char cBuf __gc[] = new unsigned char __gc[256];
>
> ...
>
> // Call to new .NET C# component
> m_pCommInt->SendCommand(cMessage, &cResult ) ;
>
> ...
>
> strncpy((unsigned char*)&cBuf[0], ((unsigned char*)&cResult[0]) + 3,
> (int)cResult[2]);
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> The last line causes error C2440: 'type cast' : cannot convert from
> 'unsigned char __gc *' to 'unsigned char *' Cannot convert a managed
> type to an unmanaged type
>
> I understand that strncpy doesn't understand managed types, but it means
> that in
> this case we won't be able easily migrate our old app to .NET platform.
> Lots
> of
> changies will be required to old app to make it interoperate with new .NET
> C# component.
>
> Are there other ways to make old VC++ 6.0 app to interoperate with new
> .NET
> C# component?
>
> Can anyone suggest a better way for old VC++ 6.0 app to interoperate with
> new .NET C# component?
>
> Can anyone suggest recommend a good example of old VC++ 6.0 app
> interoperating with new .NET C# component?
>
> Can anyone clearly explain how to fix code segment above with strncpy
> function causing C2440 error?
>
> Can anyone provide good examples how to convert array array of managed
> types
> to unmanaged e.g. unsigned char cManaged __gc[] to unsigned char
> cUnmanaged[256]?
>
> Most of the examples and methods in MSDN are about other way around
> situation when new .NET app calls old legacy component (P/Invoke etc).



Relevant Pages

  • Re: Direct3D ExecutionEngineException
    ... I managed to get the VertexBuffer and IndexBuffer built without ... shutting down the app by reducing the index buffer array to the exact size ... app quits. ...
    (microsoft.public.win32.programmer.directx.managed)
  • Re: Application consumes close to 100% of CPU, how to profile and determine the cause
    ... you don't need to pin the byte array before passing ... > For the P/Invoke functions they look like the following ... > particular time and causing the problem. ...
    (microsoft.public.dotnet.languages.csharp)
  • Problem with Array from C# to COM
    ... app is an Application-Object of the Library. ... The array id has two ... Dim app, broadcast, id ... Set app = CreateObject ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Can I use an array with an IN clause?
    ... I tried passing an array of integers as the parameter, ... I can solve the problem by generating a query of the form "WHERE ID IN ... because the number of integers varies. ... Without knowing more about your app it is a bit hard to say what you ...
    (comp.lang.java.databases)
  • Re: Passing Array with CreateThread?
    ... is the entirety of my main program, so if the app is running, the main ... passing an entire array that way for some reason .. ... As I said, it works fine if you just call it as a sub, so its obviously ... Public Sub TestThread ...
    (microsoft.public.vb.general.discussion)

Loading