编译c#应用程序:错误:预期的标识符

时间:2009-04-25 22:40:21

标签: c#

任何人都可以告诉我为什么用包含以下代码的.cs文件编译C#应用程序会给我一个错误。见下文。

namespace defintions
{
    unsafe public struct name
    {
       char* firstname;
       char* lastname;
    } ;

   class Functions
    {
       [DllImport("C++Dll.dll")]
       public unsafe static extern long func(name *);    //error : Identifier expected

    }

 }

2 个答案:

答案 0 :(得分:8)

您的函数的参数没有名称(名称*不是名称)。

将其更改为

[DllImport("C++Dll.dll")]
       public unsafe static extern long func(name* theName);    //error : Identifier expected

答案 1 :(得分:3)

您不必在示例中使用不安全的代码。总是通过引用非托管代码来编组类。试试这个:

namespace defintions
{    
    public class name    
    {       
        [MarshalAs(UnmanagedType.LPStr)]
        string firstname;
        [MarshalAs(UnmanagedType.LPStr)]
        string lastname;    
    } 


    class Functions    
    {       
        [DllImport("C++Dll.dll")]       
        public static extern long func(name theName);   
    } 
}