C# - form window random class name

Alexandru Teodor 91 Reputation points
2022-07-18T05:06:29.867+00:00

In a VS 2019 WindowsFormsApp, using C#, I am looking for ways to prevent automation of my window form.

I know that an application can be targeted by process name, window title and window class (I hope I didn't missed another way).

The process name and the window title I can change for each user, but I have no ideea how I can change the window class name which is the same every time I start the app.

Is there a way to change this window class name to something "random" everytime the app starts?

Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} vote

4 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,591 Reputation points Volunteer Moderator
    2022-07-18T06:48:44.083+00:00

    No you can not change the class name. If you have users who are that sophisticated then consider looking into copy protection and obfuscation using a tool like Obfuscar (free).

    1 person found this answer helpful.

  2. Castorix31 91,406 Reputation points
    2022-07-18T07:46:34.507+00:00

    You can use your own class name with RegisterClassEx, then CreateWindow, like for any C++/Win32 app
    But it won't prevent Automation...

    1 person found this answer helpful.
    0 comments No comments

  3. Alexandru Teodor 91 Reputation points
    2022-07-20T05:16:20.923+00:00

    @Castorix31 , since your reply, I have been trying to find examples online and tryed to implement this. Here createwindowex-and-registerclass-c-sharp is the best I could find, but still I cannot get it to work

    Is it true that there is no way to integrate the RegisterClassEx with the standard WindowsFormsApp template generated by VS 2019? You have to code the window and its from scratch without the designer?

    I tryed the following:

    1. Started a new Windows Forms App (.NET Framework) project.
    2. in the generated template Form1.cs and I added at the top of the "public partial class Form1 : Form" the following code:
          [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
          struct WNDCLASSEX
          {
              [MarshalAs(UnmanagedType.U4)]
              public int cbSize;
              [MarshalAs(UnmanagedType.U4)]
              public int style;
              public IntPtr lpfnWndProc;
              public int cbClsExtra;
              public int cbWndExtra;
              public IntPtr hInstance;
              public IntPtr hIcon;
              public IntPtr hCursor;
              public IntPtr hbrBackground;
              [MarshalAs(UnmanagedType.LPStr)]
              public string lpszMenuName;
              [MarshalAs(UnmanagedType.LPStr)]
              public string lpszClassName;
              public IntPtr hIconSm;
          }
      
          [DllImport("user32.dll", SetLastError = true, EntryPoint = "CreateWindowEx")]
          public static extern IntPtr CreateWindowEx(
             int dwExStyle,
             //UInt16 regResult,
             [MarshalAs(UnmanagedType.LPStr)]
         string lpClassName,
             [MarshalAs(UnmanagedType.LPStr)]
         string lpWindowName,
             UInt32 dwStyle,
             int x,
             int y,
             int nWidth,
             int nHeight,
             IntPtr hWndParent,
             IntPtr hMenu,
             IntPtr hInstance,
             IntPtr lpParam);
      

    After that I don't know how to proceed...

    Can the above code somehow modifiy the window generated by the default template and continue with the designer?


  4. Alexandru Teodor 91 Reputation points
    2022-07-20T09:47:22.083+00:00

    @Castorix31 , Now I think I understand why you said that changing the window class name won't prevent automation. They could create a tool that triggers directly the controls on the form ignoring the window title or class :( I haven't even thought about the class of the controls. All my efforts were just to change the window class name.

    However, If I wanted to change just the window class to a custom one, but still be able to use the designer and create controls with it, would it be possible?

    Is it possible that in the tempate of a standard WindowsForms App to modify just the class of the window, and then continue with the designer creating controls with whatever class vs generates for them? (something like "injecting" the custom class in the standard template code wihout careing what class the designer generated controls have....)


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.