.NET

.NET

Reply
Active Contributor
48 Posts
1 Kudo
Registered: ‎11-28-2012
Post 1 of 8
Accepted Solution

Determine 32- or 64-bit application

2150 Views, 7 Replies
01-07-2013 07:31 AM

I am using a dllimport from accore.dll, but the entry point for the method in the dll differs between 32-bit and 64-bit version of AutoCAD. How can I check whether the AutoCAD application is a 32- or 64-bit version, i.e. the dll is 32- or 64-bit?

Hi Alexander,

 

public static bool IsX64()
{
return IntPtr.Size == 8;
}

is less verbose and quite self explanatory.

Sample that demonstrate common source code for different AutoCAD version and Platform (32 or 64 bit):

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using AcAp = Autodesk.AutoCAD.ApplicationServices;
[assembly: CommandClass(typeof(Rivilis.HookESC))]
namespace Rivilis
{
public static class HookESC
{
static int AcadVer = AcAp.Application.Version.Major;
// For AutoCAD 2007 - 2012 64 bit
[DllImport("acad.exe", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "?acedRegisterFilterWinMsg@@YAHQ6AHPEAUtagMSG@@@Z@Z")]
private static extern int acedRegisterFilterWinMsg2012x64(WindowHookProc callBackFunc);
[DllImport("acad.exe", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "?acedRemoveFilterWinMsg@@YAHQ6AHPEAUtagMSG@@@Z@Z")]
private static extern int acedRemoveFilterWinMsg2012x64(WindowHookProc callBackFunc);
// For AutoCAD 2007 - 2012 32 bit
[DllImport("acad.exe", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "?acedRegisterFilterWinMsg@@YAHQ6AHPAUtagMSG@@@Z@Z")]
private static extern int acedRegisterFilterWinMsg2012x32(WindowHookProc callBackFunc);
[DllImport("acad.exe", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "?acedRemoveFilterWinMsg@@YAHQ6AHPAUtagMSG@@@Z@Z")]
private static extern int acedRemoveFilterWinMsg2012x32(WindowHookProc callBackFunc);
// For AutoCAD 2013 64 bit
[DllImport("accore.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "?acedRegisterFilterWinMsg@@YAHQ6AHPEAUtagMSG@@@Z@Z")]
private static extern int acedRegisterFilterWinMsg2013x64(WindowHookProc callBackFunc);
[DllImport("accore.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "?acedRemoveFilterWinMsg@@YAHQ6AHPEAUtagMSG@@@Z@Z")]
private static extern int acedRemoveFilterWinMsg2013x64(WindowHookProc callBackFunc);
// For AutoCAD 2013 32 bit
[DllImport("accore.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "?acedRegisterFilterWinMsg@@YAHQ6AHPAUtagMSG@@@Z@Z")]
private static extern int acedRegisterFilterWinMsg2013x32(WindowHookProc callBackFunc);
[DllImport("accore.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "?acedRemoveFilterWinMsg@@YAHQ6AHPAUtagMSG@@@Z@Z")]
private static extern int acedRemoveFilterWinMsg2013x32(WindowHookProc callBackFunc);
private static int acedRegisterFilterWinMsg(WindowHookProc callBackFunc)
{
if (AcadVer >= 19) {
if (IntPtr.Size == 4) return acedRegisterFilterWinMsg2013x32(callBackFunc);
else return acedRegisterFilterWinMsg2013x64(callBackFunc);
} else {
if (IntPtr.Size == 4) return acedRegisterFilterWinMsg2012x32(callBackFunc);
else return acedRegisterFilterWinMsg2012x64(callBackFunc);
}
}
private static int acedRemoveFilterWinMsg(WindowHookProc callBackFunc)
{
if (AcadVer >= 19) {
if (IntPtr.Size == 4) return acedRemoveFilterWinMsg2013x32(callBackFunc);
else return acedRemoveFilterWinMsg2013x64(callBackFunc);
}
else {
if (IntPtr.Size == 4) return acedRemoveFilterWinMsg2012x32(callBackFunc);
else return acedRemoveFilterWinMsg2012x64(callBackFunc);
}
}
// hook message filter callback function
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int WindowHookProc(ref Message msg);
private static WindowHookProc callBackFunc = null;
private static int WindowsHook(ref Message msg)
{
if (msg.Msg == 0x0100 && msg.WParam == (IntPtr)0x1b) {
// MessageBox.Show("ESC pressed"); // <- your's action
// acedRemoveFilterWinMsg(callBackFunc); // <- unregister callback
}
return 0;
}
[CommandMethod("SetHookEsc")]
public static void SetHookEsc()
{
callBackFunc = new WindowHookProc(WindowsHook);
acedRegisterFilterWinMsg(callBackFunc);
}
[CommandMethod("UnsetHookEsc")]
public static void UnsetHookEsc()
{
if (callBackFunc != null) acedRemoveFilterWinMsg(callBackFunc);
}
}
}

 

Moderator
1,465 Posts
277 Kudos
Registered: ‎04-09-2008
Post 2 of 8

Re: Determine 32- or 64-bit application

01-07-2013 07:51 AM in reply to: Fredrik.Larsen
public static bool IsX64()
{
   if (IntPtr.Size == 4) return false;
             else        return true;
}

 Sample of using: http://forums.autodesk.com/t5/NET/Can-t-find-ModelessOperationWill-Start-Event-in-Dot-net/m-p/344769...

Other example: http://forums.autodesk.com/t5/NET/Save-a-password-protected-drawing-without-password-with-VB-NET/m-p...


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

*Expert Elite*
2,140 Posts
253 Kudos
Registered: ‎04-29-2006
Post 3 of 8

Re: Determine 32- or 64-bit application

01-07-2013 08:42 AM in reply to: Alexander.Rivilis

Hi Alexander,

 

public static bool IsX64()
{
   return IntPtr.Size == 8;
}

is less verbose and quite self explanatory.

Gilles Chanteau
Moderator
1,465 Posts
277 Kudos
Registered: ‎04-09-2008
Post 4 of 8

Re: Determine 32- or 64-bit application

01-07-2013 01:37 PM in reply to: _gile

_gile wrote:
is less verbose and quite self explanatory.

Totally agree!


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Active Contributor
48 Posts
1 Kudo
Registered: ‎11-28-2012
Post 5 of 8

Re: Determine 32- or 64-bit application

01-08-2013 04:19 AM in reply to: Alexander.Rivilis

Hi

I didn't know about the IntPtr.size, so thank you for that information. But in my case I can't use this since I can't get IntPtr.size directly in a class where the dllimport is happening. You have to be whithin a method to use it.

 

Instead I used conditional compilation symbols and I compile one dll for 32-bit and one for 64-bit. By using the conditional compilation symbols the correct part of the code is compiled for each build. 

Moderator
1,465 Posts
277 Kudos
Registered: ‎04-09-2008
Post 6 of 8

Re: Determine 32- or 64-bit application

01-08-2013 04:30 AM in reply to: Fredrik.Larsen

Fredrik.Larsen wrote:
But in my case I can't use this since I can't get IntPtr.size directly in a class where the dllimport is happening. You have to be whithin a method to use it.

Why? Did you watch the examples to which I referred?


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Active Contributor
48 Posts
1 Kudo
Registered: ‎11-28-2012
Post 7 of 8

Re: Determine 32- or 64-bit application

01-08-2013 04:33 AM in reply to: Alexander.Rivilis

Yes, you're right. That might be easier leaving everything in one dll. 

Moderator
1,465 Posts
277 Kudos
Registered: ‎04-09-2008
Post 8 of 8

Re: Determine 32- or 64-bit application

01-08-2013 05:26 AM in reply to: Fredrik.Larsen

Sample that demonstrate common source code for different AutoCAD version and Platform (32 or 64 bit):

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using AcAp = Autodesk.AutoCAD.ApplicationServices;

[assembly: CommandClass(typeof(Rivilis.HookESC))]

namespace Rivilis
{
  public static class HookESC
  {
    static int AcadVer = AcAp.Application.Version.Major;
    // For AutoCAD 2007 - 2012 64 bit
    [DllImport("acad.exe", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl, 
      EntryPoint = "?acedRegisterFilterWinMsg@@YAHQ6AHPEAUtagMSG@@@Z@Z")]
    private static extern int acedRegisterFilterWinMsg2012x64(WindowHookProc callBackFunc);
    [DllImport("acad.exe", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl, 
      EntryPoint = "?acedRemoveFilterWinMsg@@YAHQ6AHPEAUtagMSG@@@Z@Z")]
    private static extern int acedRemoveFilterWinMsg2012x64(WindowHookProc callBackFunc);
    // For AutoCAD 2007 - 2012 32 bit
    [DllImport("acad.exe", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl, 
      EntryPoint = "?acedRegisterFilterWinMsg@@YAHQ6AHPAUtagMSG@@@Z@Z")]
    private static extern int acedRegisterFilterWinMsg2012x32(WindowHookProc callBackFunc);
    [DllImport("acad.exe", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl, 
      EntryPoint = "?acedRemoveFilterWinMsg@@YAHQ6AHPAUtagMSG@@@Z@Z")]
    private static extern int acedRemoveFilterWinMsg2012x32(WindowHookProc callBackFunc);
    // For AutoCAD 2013 64 bit
    [DllImport("accore.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl, 
      EntryPoint = "?acedRegisterFilterWinMsg@@YAHQ6AHPEAUtagMSG@@@Z@Z")]
    private static extern int acedRegisterFilterWinMsg2013x64(WindowHookProc callBackFunc);
    [DllImport("accore.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl, 
      EntryPoint = "?acedRemoveFilterWinMsg@@YAHQ6AHPEAUtagMSG@@@Z@Z")]
    private static extern int acedRemoveFilterWinMsg2013x64(WindowHookProc callBackFunc);
    // For AutoCAD 2013 32 bit
    [DllImport("accore.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl, 
      EntryPoint = "?acedRegisterFilterWinMsg@@YAHQ6AHPAUtagMSG@@@Z@Z")]
    private static extern int acedRegisterFilterWinMsg2013x32(WindowHookProc callBackFunc);
    [DllImport("accore.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl, 
      EntryPoint = "?acedRemoveFilterWinMsg@@YAHQ6AHPAUtagMSG@@@Z@Z")]
    private static extern int acedRemoveFilterWinMsg2013x32(WindowHookProc callBackFunc);

    private static int acedRegisterFilterWinMsg(WindowHookProc callBackFunc)
    {
      if (AcadVer >= 19) {
        if (IntPtr.Size == 4) return acedRegisterFilterWinMsg2013x32(callBackFunc); 
                  else        return acedRegisterFilterWinMsg2013x64(callBackFunc);
      } else {
        if (IntPtr.Size == 4) return acedRegisterFilterWinMsg2012x32(callBackFunc);
                  else        return acedRegisterFilterWinMsg2012x64(callBackFunc);
      }
    }
    private static int acedRemoveFilterWinMsg(WindowHookProc callBackFunc)
    {
      if (AcadVer >= 19) {
        if (IntPtr.Size == 4) return acedRemoveFilterWinMsg2013x32(callBackFunc);
                 else         return acedRemoveFilterWinMsg2013x64(callBackFunc);
      }
      else {
        if (IntPtr.Size == 4) return acedRemoveFilterWinMsg2012x32(callBackFunc);
                 else         return acedRemoveFilterWinMsg2012x64(callBackFunc);
      }
    }

    // hook message filter callback function
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    public delegate int WindowHookProc(ref Message msg);
    private static WindowHookProc callBackFunc = null;

    private static int WindowsHook(ref Message msg)
    {
      if (msg.Msg == 0x0100 && msg.WParam == (IntPtr)0x1b) {
        // MessageBox.Show("ESC pressed"); // <- your's action
        // acedRemoveFilterWinMsg(callBackFunc); // <- unregister callback
      }
      return 0;
    }

    [CommandMethod("SetHookEsc")]
    public static void SetHookEsc()
    {
      callBackFunc = new WindowHookProc(WindowsHook);
      acedRegisterFilterWinMsg(callBackFunc);
    }
    [CommandMethod("UnsetHookEsc")]
    public static void UnsetHookEsc()
    {
      if (callBackFunc != null) acedRemoveFilterWinMsg(callBackFunc);
    }
  }
}

 


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post