读书人

怎么将以下的C#代码转换为C++代码

发布时间: 2012-04-28 11:49:53 作者: rapoo

如何将以下的C#代码转换为C++代码?
运行环境:vs2008 wince6.0
问题: 有以下一段C#的代码,现在我需要使用C++实现同样的功能,不知道该怎么修改?


/// <summary>
/// Gets a stream handle for the given filename
/// </summary>
public static IntPtr GetStream(string filename)
{
// Get the filename in bytes
byte[] filenamebytes = System.Text.Encoding.Default.GetBytes(filename + null);

// Get a handle depending on which framework we are using
GCHandle hfile = GCHandle.Alloc(filenamebytes, GCHandleType.Pinned);

if (Environment.Version.Major == 1) return new IntPtr(hfile.AddrOfPinnedObject().ToInt32() + 4);
else return hfile.AddrOfPinnedObject();
}

调用时:

//create a handle to hold the track stream - REPLACE HERE fmod_getStream BY fmod_getStream_New TO TRY WITHOUT THE AddrOfPinnedObject BUG FIX !
IntPtr soundStreamName = Sound.GetStream(@"\NandFlash\Rolling in the Deep.mp3");

也就是说如何用C++实现GetStream()函数的功能,最好函数格式也能一致。

[解决办法]
C++不需要实现GetStream()函数,C#中需要这个函数是实现以下功能:Unicode转ANSI;.Net中的托管内存要先固定,避免被GC移动,才能传递给非托管代码使用
[解决办法]
该函数实现返回字符串的首地址,c++代码表示为:
char* GetStream(CString filename)
{
USES_CONVERSION;
char* ps = T2A(filename);
return ps;
}

调用:char* soundStreamName = GetStream(_T("\NandFlash\Rolling in the Deep.mp3"));

读书人网 >C#

热点推荐