如何将C ++函数移植到C# [英] How to port a C++ function to C#

查看:121
本文介绍了如何将C ++函数移植到C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将c ++函数移植到c#



c ++代码是



 int CCategory :: CheckFileOrFolder(CString str)
{
int iRet = -1;

try
{
const DWORD dwFileAttr = GetFileAttributes(str);

if(dwFileAttr == DWORD(-1))//找不到文件
{
iRet = -1; //没有
}
else if(dwFileAttr& FILE_ATTRIBUTE_DIRECTORY)
{
iRet = 0; //文件夹
}
其他
iRet = 1; // file
}
catch(...)
{
}

返回iRet;
}





如果我传递以下字符串,函数的输出会有所不同

 C:\ Windows \System32 \speech\speechux\sapi.cpl,-5566 





please帮助



我尝试过:



和我的c#代码是



 public int CheckFileOrFolder(string str)
{
int iRet = -1;
try
{

FileAttributes attr = System.IO.File.GetAttributes(str);
// var files = Directory.GetFiles(@c:\windows \ system32 \);

if((attr& FileAttributes.Directory)== FileAttributes.Directory)
{
iRet = 0; //文件夹
}
其他
iRet = 1; // file
}
catch(exception ex)
{
//找不到文件
iRet = -1; //没有
}

返回iRet;
}

解决方案

您的输入不是有效路径,而是添加, - 5566的路径。在这种情况下, GetAttributes C函数可能返回一些值使用if / else代码无效但 System.IO.File.GetAttributes 正确抛出异常。



使用调试器查看详细信息以查找问题。


 CheckFileOrFolder(@ C:\Windows\System32\speech\speechux\sapi.cpl); 





返回== 1用c ++和C#






您可以使用此解决方案。



您也可以使用此IDE:免费产品版 [ ^ ]



 public int CheckFileOrFolder(string str)
{
int iRet = - 1;
try
{

FileAttributes attr = System.IO.File.GetAttributes(str);
// var files = Directory.GetFiles(@c:\windows \ system32 \);

if((attr& FileAttributes.Directory)== FileAttributes.Directory)
{
iRet = 0; //文件夹
}
其他
{
iRet = 1; // file
}
}
catch(Exception ex)
{
//找不到文件
iRet = -1;
}

返回iRet;
}


iam porting a c++ function to c#

code of c++ is

int CCategory::CheckFileOrFolder(CString str)
{
	int iRet = -1;

	try
	{
		const DWORD dwFileAttr = GetFileAttributes(str);

		if (dwFileAttr == DWORD(-1)) // file not found
		{
			iRet = -1;		// nothing
		} 
		else if (dwFileAttr & FILE_ATTRIBUTE_DIRECTORY)
		{
			iRet = 0;		//folder
		}
		else	
			iRet = 1;		// file
	}
	catch(...)
	{
	}

	return iRet;
}



the output of the function is different if i pass following string

C:\Windows\System32\speech\speechux\sapi.cpl,-5566



please help

What I have tried:

and my c# code is

public int CheckFileOrFolder(string str)
        {
            int iRet = -1;
            try
            {

                FileAttributes attr = System.IO.File.GetAttributes(str);
                //var files = Directory.GetFiles(@"c:\windows\system32\");

                if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
                {
                    iRet = 0;		//folder
                }
                else
                    iRet = 1;		// file
            }
            catch (Exception ex)
            {
                //File not found
                iRet = -1; // nothing
            }

            return iRet;
        }

解决方案

Your input isnt a valid path, but a path with the addition ",-5566". In that case the GetAttributes C function may return some value which doesnt work correct with the if/else code but System.IO.File.GetAttributes is correctly throwing an exception.

Use the debugger to look at the details to find the problem.


CheckFileOrFolder(@"C:\Windows\System32\speech\speechux\sapi.cpl");



return == 1 with c++ and C#


Hi ,

You can use this solution .

also you can use this IDE : Free Product Editions[^]

public int CheckFileOrFolder(string str)
{
			int iRet = -1;
			try
			{

				FileAttributes attr = System.IO.File.GetAttributes(str);
				//var files = Directory.GetFiles(@"c:\windows\system32\");

				if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
				{
					iRet = 0; //folder
				}
				else
				{
					iRet = 1; // file
				}
			}
			catch (Exception ex)
			{
				//File not found
				iRet = -1; 
			}

			return iRet;
}


这篇关于如何将C ++函数移植到C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆