IF within a class

2

I am new to C #, and wanted to do the following: I have the dllimport class being used in another class. The problem is this, I want to pass the constant (dll path) according to the version of windows 64 or 32. I tried to pass a method as parameter, but it did not scroll and from what I saw the class dllimport only accepts static (or constant) argument. Anyone know how to solve it? Here is 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);
    
asked by anonymous 12.09.2017 / 21:47

2 answers

1

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

 #if WIN64
   const string DllName = "\EZForecourt\EZClient64.dll";
 #else
   const string DllName = "\EZForecourt\EZClient.dll";
 #endif
    
13.09.2017 / 18:56
3

I've been through this same problem a while ago, because I developed for both types, to solve this I needed to add a "DLL" folder to my project and added both types of dll to it.

ThenIlookedforawaytobeabletosolvethisproblemalwaysloadingthecorrectdllwhenstartingtheapplication.

Class

publicstaticclassDLL_Loader{///<summary>///VerificaaarquiteturadoSO(32/64bits)ecarregaadllcorretaparafuncionamentodasimpressorasTSC.///</summary>publicstaticvoidTSC_DLL(stringappLocation){//stringappLocation=Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);stringdllLocation=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.

    
12.09.2017 / 22:04