Jump to content
Sign in to follow this  
jcddcjjcd

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

Recommended Posts

jcddcjjcd

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

Share this post


Link to post
Share on other sites
Richard Robertson

Not the best solution but a solution nonetheless. I would have linked both dlls in code and used some kind of lookup at runtime to call the appropriate version (32 versus 64).

Share this post


Link to post
Share on other sites
jcddcjjcd

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

Share this post


Link to post
Share on other sites
Richard Robertson

I'll have to look at it when I get home from work later.

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.