Search code examples
c#namespacespinvokedllimporttapi

invalid token namespace, type or namespace not found, invalid token "{"


I want to call anybody from my application and find this method but it doesn't work..

    namespace SysWin32
    { 
        class programm
        {
            [DllImport("Tapi32.dll")]
            public static extern long tapiRequestMakeCall(string Number, string AppName, string CalledParty, string Comment);
        }

    }

Solution

  • The code in your question compiles perfectly, assuming that you place that code in the correct place. The compiler error you report is related to code that is not present in the question.

    Perhaps you have nested the code in the question inside another class. For instance this code:

    namespace ConsoleApplication1
    {
        class Program
        {
            namespace SysWin32
            {
                class programm
                {
                    [DllImport("Tapi32.dll")]
                    public static extern long tapiRequestMakeCall(string Number, 
                        string AppName, string CalledParty, string Comment);
                }
    
            }
    
            static void Main(string[] args)
            {
            }
        }
    }
    

    results in errors similar to those that you report. Of course, I am having to guess as to what your real code is. Perhaps it is a variant on this. No matter, the key point is that the code in the question is fine, but the problem is in the code surrounding it, the code that we cannot see.

    I would also point out that the function return type is wrong. In C and C++ on Windows, long is a 32 bit signed integer. In C#, long is a 64 bit signed integer. So the return value type for tapiRequestMakeCall should be declared as int in your C# p/invoke declaration.

    The following code will compile:

    namespace ConsoleApplication1
    {
        class programm
        {
            [DllImport("Tapi32.dll")]
            public static extern int tapiRequestMakeCall(string Number, 
                string AppName, string CalledParty, string Comment);
    
            static void Main(string[] args)
            {
            }
        }
    }