IF within a class

Asked

Viewed 294 times

2

I’m new to c#, and I wanted to do this: I have the dllimport class that is being used in another class. The problem is this, I want to pass the constant(dll path) according to the version windows 64 or 32. I tried to pass a method as parameter, but it did not roll and so I saw the dllimport class only accepts static(or constant) argument. Does anyone know how I solve it? Follows my code:

//const string DllName = "C:\\EZForecourt\\EZClient.dll";
public string DllName()
{

if (Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE") == "AMD64" || Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432") == "AMD64")
{
    return "C:\\EZForecourt\\EZClient64.dll";
}
else 
{
    return "C:\\EZForecourt\\EZClient.dll";
}

}    

//--------------------------------- Connection -----------------------------------------//

[DllImport(DllName, CharSet = CharSet.Unicode)]
internal static extern Int32 ClientLogon(Int32 ClientID, Int16 ClientType, Int32 EventHandle, System.IntPtr hWnd, Int32 wMsg);

2 answers

3

I went through this same problem a while ago as I develop for both types, to solve this I needed to add to my project a "DLL" folder and in it I added both types of dll.

Imagem da pasta DLL

So I looked for a way to solve this problem by always carrying the dll correct when starting the application.

Class

    public static class DLL_Loader
    {
        /// <summary>
        /// Verifica a arquitetura do SO(32/64 bits) e carrega a dll correta para funcionamento das impressoras TSC.
        /// </summary>
        public static void TSC_DLL(string appLocation)
        {
            //string appLocation = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
            string dllLocation = Path.Combine(appLocation, @"Dll\TSC\TSCLIB_32B.dll");
            string newLocation = Path.Combine(appLocation, @"TSCLIB.dll");

            if (IntPtr.Size == 8) //No modo 32bit o IntPrt.Size será 4, já em 64bit será 8.
            {
                dllLocation = Path.Combine(appLocation, @"Dll\TSC\TSCLIB_64B.dll");
            }

            File.Copy(dllLocation, newLocation, true); //Gerar dll correta com o nome que preciso.
        }
    }

To call her:

DLL_Loader.TSC_DLL(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location));

I use in class Program.cs of my projects WindowsForm.

I hope I can help you.

1


Gonçalves, thanks again for the help, but I found a simpler way to solve it. I put a directive:

 #if WIN64
   const string DllName = "\\EZForecourt\\EZClient64.dll";
 #else
   const string DllName = "\\EZForecourt\\EZClient.dll";
 #endif

Browser other questions tagged

You are not signed in. Login or sign up in order to post.