2

Is it possible to detect if a winform project's target platform is set to AnyCPU using C# programming language during design mode?

For example, creating a button control that, when clicked, will determine if the project's target platform is set to AnyCPU, x86 or x64?

This should be detected while in design mode by a hosted control, e.g. a button click determining the target platform of the project it is being used in.

The language of use is C#.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Russell Chidhakwa
  • 339
  • 2
  • 5
  • 16

2 Answers2

2

You can add a reference to EnvDTE and add such a property to your control:

[EditorBrowsable(EditorBrowsableState.Never)]
public string TargetPlatform
{
    get
    {
        if (!DesignMode)
            return null;

        var host = (IDesignerHost)Site.GetService(typeof(IDesignerHost));
        var dte = (EnvDTE.DTE)host.GetService(typeof(EnvDTE.DTE));
        var project = dte.ActiveSolutionProjects[0];
        return project.ConfigurationManager.ActiveConfiguration.Properties
                      .Item("PlatformTarget").Value;
    }
}

Note: The answer is a PoC showing the solution works. For a real world scenario, it should be a design-time only property of the designer of the control in a separate assembly. Then you don't need to distribute additional assemblies.

Also the [Designer] attribute should use name of the types rather than type itself. It's the same way that windows forms designers work. You don't need to distribute additional design-time assemblies along with your application, however as part of the nuget package or VSIX of your control installer, they should be distributed to work in VS.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
0
  if (IntPtr.Size != 8)
  {
    //64-bit process.
  }


  if (IntPtr.Size != 4)
  {
   //32-bit process
  }
Joeri E
  • 111
  • 9
  • @Joeri E: let me try that. – Russell Chidhakwa Jan 24 '20 at 08:15
  • @RezaAghaei not sure since AnyCpu means x64 on 64-bit platform and x32 on 32bit platform. I'm afraid that AnyCpu can't be properly determined in any way except of reading active project build configuration. – oleksa Jan 24 '20 at 08:17
  • By default, Visual Studio IDE sets the target platform to AnyCPU and the developer can change to 32 or 64 bit or revert to AnyCPU. I want to read that setting – Russell Chidhakwa Jan 24 '20 at 08:21
  • @RussellChidhakwa then the following link is what you need: https://stackoverflow.com/questions/25035510/programmatically-read-target-platform-during-run-time – Joeri E Jan 24 '20 at 08:26
  • @Joeri E: the SO answer refers to runtime – Russell Chidhakwa Jan 24 '20 at 08:34
  • @RussellChidhakwa the `#if PLATFORM_X86` from the answer should work in design time too. I can see how in the design time `#if DEBUG` code is working in the Debug and does not executed in the Release. – oleksa Jan 24 '20 at 08:53
  • These are conditional compilation symbol. It means when you release and distribute your control, the compiled code doesn't include those symbols and instead the compiled code will include the chosen statements as part of release. For example, your distributed control will always show "x86" if the statement has been resolved to x86 at release time. So even if it's promising in the first glance, it's not reliable. – Reza Aghaei Jan 24 '20 at 11:20