Click here to Skip to main content
15,890,670 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, i'm pretty new at C# (using VB.Net before), i wanted to use method from Benoit Blanchon from this post:
.net - Using a 32bit or 64bit dll in C# DllImport - Stack Overflow[^]

But i'm not sure about
Quote:
can be done in the static constructor of the parent class.


Just guessing, is it supposed to add the code around the Program.cs? as i know that's where the app starts. if so, where should I put the code? because it seems the code is not detected when I put it above/below [STAThread], even the app unable to start if I modify the Program.cs a lot :).

my default Progarm.cs
C#
namespace TestApp
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
?... 
Debug.WriteLine("Wanted to make sure if the DLL Loaded");
?... 
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}


Thanks in advance.

What I have tried:

Tried to place the code in Form1.cs, also googling about static constructor / parent class too, but still not figured it.
Posted
Updated 28-Oct-21 12:39pm
Comments
PIEBALDconsult 28-Oct-21 18:40pm    
As it says, in a static constructor of the Program class.

1 solution

I think you're looking for, though this may not work at all. You may have to move the code to a class separate from Program: (I don't have time to test this)
C#
namespace TestApp
{
    static class Program
    {

        ... Your DllImport line(s) for the test
        ... function you want to call goes here

        static Program()
        {
            ... Your architecture determination 
            ... and dll loading code goes here
        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            ... Your "test" call to the functions in the DLL goes here

            Debug.WriteLine("Wanted to make sure if the DLL Loaded");

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900