How to handle resources in mixed managed/unmanaged C++ projects?

G

Guest

I have former VC++ 6.0 application which was ported to .NET.
Now we continue to develop this app under VS.NET. The app
now contains both managed and unmanaged classes.

Now I have a problem with usage of resources in managed classes.
For example I want to display warning message and want to load
that warning message from the resource file. The problem is that
in this mixed managed/unmanaged project I don't have pure managed
resources (like for example in C#) and I can't use ResourceManager
to get strings from resources.

The project still has old unmanaged resource file (.rc)
So the only option I see is the following:

// Load from unmanaged resource file
CString strMsg ;
strMsg.LoadString(IDS_READ_PROGRAM_STATUS ) ;

// Convert to managed string
String* strMsgManaged = Marshal::ptrToStringAnsi(static_cast<IntPtr>(strMsg
) ) ;

This approach looks very ugly to me.

So here are my questions:

Is there simple way to load string from unmanaged resource file to managed
String object?

What are the best practices to handle resources in mixed managed/unmanaged
C++ projects?

Thanks
 
J

Jochen Kalmbach [MVP]

Hi said:
The project still has old unmanaged resource file (.rc)
So the only option I see is the following:

// Load from unmanaged resource file
CString strMsg ;
strMsg.LoadString(IDS_READ_PROGRAM_STATUS ) ;

// Convert to managed string
String* strMsgManaged = Marshal::ptrToStringAnsi(static_cast<IntPtr>(strMsg
) ) ;

This approach looks very ugly to me.
Yes.

So here are my questions:

Is there simple way to load string from unmanaged resource file to managed
String object?

Why not just do:

String *strMsgManaged = strMsg;

?

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
G

Guest

Agree,
String *strMsgManaged = strMsg;
looks much better, but the main question how to load strings from unmanaged
resource file to managed String object remained unanswered ...
 
J

Jochen Kalmbach [MVP]

Hi Steve!
String *strMsgManaged = strMsg;
looks much better, but the main question how to load strings from unmanaged
resource file to managed String object remained unanswered ...

This is exactly what you are doing, isn´t it?

What answer do you expect?

In managed world you can use "ResourceManager" to load strings from
managed-resources (resx).
In unmanaged world you can use LoadString...

There is no world between them...

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top