.NET

Reply
Active Contributor
Fredrik.Larsen
Posts: 46
Registered: ‎11-28-2012
Message 1 of 8 (1,710 Views)
Accepted Solution

Determine 32- or 64-bit application

1710 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?

Moderator
Alexander.Rivilis
Posts: 1,388
Registered: ‎04-09-2008
Message 2 of 8 (1,707 Views)

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*
_gile
Posts: 2,069
Registered: ‎04-29-2006
Message 3 of 8 (1,695 Views)

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
Alexander.Rivilis
Posts: 1,388
Registered: ‎04-09-2008
Message 4 of 8 (1,676 Views)

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
Fredrik.Larsen
Posts: 46
Registered: ‎11-28-2012
Message 5 of 8 (1,645 Views)

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
Alexander.Rivilis
Posts: 1,388
Registered: ‎04-09-2008
Message 6 of 8 (1,640 Views)

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
Fredrik.Larsen
Posts: 46
Registered: ‎11-28-2012
Message 7 of 8 (1,638 Views)

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
Alexander.Rivilis
Posts: 1,388
Registered: ‎04-09-2008
Message 8 of 8 (1,628 Views)

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

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community