Jump to content

Here is how to use 32bit and 64bit AutoITX3 in AnyCPU dotnet compliations


jcddcjjcd
 Share

Recommended Posts

I recently decided to give all my applications the ability to load on either 32 or 64 bit machines. Here I present my way of loading the correct dll for the operating system being targeted. If you are not using the AnyCPU compile option and are targeting 32 or 64 directly then this is not needed but you have to maintain 2 development versions.

First thing is to make all your declarations like this:

[DllImport("AutoItX3_targetted.dll", SetLastError = true, CharSet = CharSet.Auto)]

static public extern void AU3_Init();

And to make sure you have the correct dll loaded put this in the MainForm.Load

//So that we can target 32 and 64bit system with AnyCPU we need to provide the correct AutoItX3 dll depending on platform.

//We can't refference both of them.

string ExecutingAssemblyPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

string fullPath = "";

string newFilePath = Path.Combine(ExecutingAssemblyPath, "AutoItX3_targetted.dll");

if (IntPtr.Size == 4)

{

fullPath = Path.Combine(ExecutingAssemblyPath, "AutoItX3.dll");

}

else

{

fullPath = Path.Combine(ExecutingAssemblyPath, "AutoItX3_x64.dll");

}

if (System.IO.File.Exists(fullPath))

{

try

{

System.IO.File.Copy(fullPath, newFilePath, true);

}

catch (Exception ex)

{

Logger.WriteLog("AutoItX3_targetted.dll Creation Failure!", ex);

MessageBox.Show("AutoItX3_targetted.dll Creation Failure!\r\nApplicaction will Exit");

Application.Exit();

}

}

So the code always refers to "AutoItX3_x64.dll"

In the Visual Studio Solutions Explorer you mark the dll's as Always Copy in Properties.

Hope this helps.

Regards,

John.

PS. The 64 bit AutoItX3 works very well in Windows 7 64bit and I did not need to make any changes to the AutoItX3Declarations.cs I presented in the post shown below except to change "AutoItX3.dll" to "AutoItX3_x64.dll"

http://www.autoitscript.com/forum/index.php?showtopic=72905&hl=jcddcjjcd&st=0

Link to comment
Share on other sites

I first tried this kind of approach:

internal static void AU3_Init()

{

if(IntPtr.Size == 4)

{

AutoItX3Declarations32.AU3_Init();

}

else

{

AutoItX3Declarations64.AU3_Init();

}

}

but that does not work because the call to the other dll is partly resolved at runtime and causes an exception.

I searched for hours to find a more elegant solution but they were all much uglier. This works nicely and is simple with the advantage of easy updates to the dll's without having to recompile the applications as would be the case if the dll's were embedded in an other assembly which was conditionally loaded.

Perhaps others will have some ideas.

Regards,

John.

Edited by jcddcjjcd
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...