none
How to find signiture / entry point of native exe RRS feed

  • Question

  • Hi,

    For a test, I wrote a "hello world" kind of VC++ program in VS2017 as below. 

    // VCPPConsoleApplication1.cpp : Defines the entry point for the console application.
    #include "stdafx.h"
    int main()
    {
     printf("This is VC++ using VS2017");
        return 10;
    }

    Now in WPF I am trying to call main() function using pinvoke.

      [DllImport("VCPPConsoleApplication1.exe",  EntryPoint ="??????? What should be here ???? ")]
      public static extern int main();


    private void btnPinvoke_Click(object sender, RoutedEventArgs e)

      {
         var i = main();
      }

    But it gives error at runtime :  System.EntryPointNotFoundException: 'Unable to find an entry point named 'main' in DLL 'VCPPConsoleApplication1.exe'.'

    Any advice. ?

    Regards

    Monday, December 4, 2017 5:17 PM

Answers

  • Hello Richard,

    >>I was able to successfully call JustAFunction( ) , but while calling main( ) it failed with error "Attempted to read or write protected memory. This is often an indication that other memoryis corrupt"

    This is so difficult for CLR to run the main method. When CLR run the main method of exe file. The window OS will prepare another process job for starting. but the compiled code is located at the current process memory. And windows doesn't allow one process access memory of others directly. So your code can't be corrected. I strongly suggest you invoke dll files rather than exe files. When you define a dll file. You could name any method as you want.

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 6, 2017 8:58 AM
    Moderator

All replies

  • The parameter of the DllImport attribute constructor needs to be the name of a DLL library file. It doesn't work with a EXE file

    Maybe, you could run your C++ exe file from C# as a process

    wizend

    Monday, December 4, 2017 5:54 PM
  • A DLL is executed in the same address space (process) as its user (caller). An exe is executed in its own (new) address space. You might be able to use an exe as a DLL but you need to know more about how the compiler and the generated exe works; one of the important things is that the C runtime must be initialized.

    I assume you want to use DllImport in an effort to simplify the exchange of data. I can't think of any other reason to do that. However the arguments to the main function are passed from the OS (Windows) differently than the way that another program would.



    Sam Hobbs
    SimpleSamples.Info

    Monday, December 4, 2017 7:24 PM
  • Hello richard,

    EntryPoint is the method name which value should match your function name in your C++ file. When you don't define the entrypoint, the methodname of next line should match C++ function so that the CLR knows which method should invoke. You should create a C++ dll rather than exe file. Some keywords indicates the method could invoke outside.

    #include "stdafx.h"
    
    extern "C" __declspec(dllexport) int main()
    {
    	printf("This is VC++ using VS2017");
    	return 10;
    }

    Build it and copy the dll file under bin folder of your C# program.

    class Program
        {
            [DllImport("Test.dll",EntryPoint ="main")]
            public static extern int test();
    
    
            static void Main(string[] args)
            {
                int result = test();
                Console.WriteLine("\n"+result);
                Console.Read();
            }
        }

    The result.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, December 5, 2017 7:13 AM
    Moderator
  • Thanks Sam Hobbs , Neil Hu.

    Neil Hu, Your input did help with my further experiments.  I slightly modified my console app as below. 

    // VCPPConsoleApplication1.cpp : Defines the entry point for the console application. // #include "stdafx.h" static int f = 10; extern "C" __declspec(dllexport) int JustAFunction();

    extern "C" __declspec(dllexport) int main();

    extern "C" __declspec(dllexport) int main() { printf("This is VC++ using VS2017 MSFT"); int i = JustAFunction(); return 10; } extern "C" __declspec(dllexport) int JustAFunction() { return f + 100; }

    and then tried calling it in wpf as below.

    [DllImport("VCPPConsoleApplication1.exe", CallingConvention = CallingConvention.Cdecl)]
    public static extern int main();
    
    [DllImport("VCPPConsoleApplication1.exe")]
    public static extern int JustAFunction();
    
    private void btnPinvoke_Click(object sender, RoutedEventArgs e)
    {
    int i = JustAFunction();
    i = main();
    }

    I was able to successfully call JustAFunction( ) , but while calling main( ) it failed with error "Attempted to read or write protected memory. This is often an indication that other memory is corrupt"

    How can I correct this ?  Any Advice ?

    The reason behind these experiments is to run an external EXE from the main application (which will be Store App) and currently we are doing it successfully, using Process.Start() , but we now know that such store apps will fail in "Windows 10 S" version of windows and we want to find a workaround for this restriction. 


    • Edited by Richard Freytag Tuesday, December 5, 2017 5:10 PM mising main() declaration line
    Tuesday, December 5, 2017 5:03 PM
  • I was able to successfully call JustAFunction( ) , but while calling main( ) it failed with error "Attempted to read or write protected memory. This is often an indication that other memory is corrupt"

    How can I correct this ?  Any Advice ?

    I assume the problem is that the C runtime is not initialized. You need to analyze the internals of the code generated by the compiler and the use of the C runtime.

    The reason behind these experiments is to run an external EXE from the main application (which will be Store App) and currently we are doing it successfully, using Process.Start() , but we now know that such store apps will fail in "Windows 10 S" version of windows and we want to find a workaround for this restriction. 


    There are documented and supported IPC mechanisms. Attempts to bypass any limitations on store applications are likely to cause the application to be rejected, right? So even if you are successful in executing a function the way you are trying to you will fail in the attempt to use it in the store application. You need to solve the problem in a manner that is supported by Microsoft.



    Sam Hobbs
    SimpleSamples.Info

    Tuesday, December 5, 2017 6:52 PM
  • Hi, 

    Any pointers to "Supported IPC mechanisms" 

    Regards

    Wednesday, December 6, 2017 8:57 AM
  • Hello Richard,

    >>I was able to successfully call JustAFunction( ) , but while calling main( ) it failed with error "Attempted to read or write protected memory. This is often an indication that other memoryis corrupt"

    This is so difficult for CLR to run the main method. When CLR run the main method of exe file. The window OS will prepare another process job for starting. but the compiled code is located at the current process memory. And windows doesn't allow one process access memory of others directly. So your code can't be corrected. I strongly suggest you invoke dll files rather than exe files. When you define a dll file. You could name any method as you want.

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 6, 2017 8:58 AM
    Moderator
  • A great place to look is here.


    Sam Hobbs
    SimpleSamples.Info

    Wednesday, December 6, 2017 5:04 PM