flat assembler
Message board for the users of flat assembler.

Index > High Level Languages > A simple DLL (retrieve basic CPU info) for P/Invoke

Author
Thread Post new topic Reply to topic
FlierMate11



Joined: 13 Oct 2022
Posts: 94
FlierMate11 01 Feb 2023, 13:27
Quote:
Discussion on high level language compilers using flat assembler as a backend....


Yes, although it is not the same nature, but I am talking about usage of DLL in high-level language, so I think I will 'advertise' here. Hehe.

This DLL returns two information only, processor name string, and AVX support status (taken from Tomasz's code example)
Image

Image

This is how to call the DLL function from C# .NET: (Must set x86 Debug or Release mode, don't use "Any CPU", or else it complains error "BadImageFormatException")

Code:
using System.Runtime.InteropServices;
using System.Text;

namespace CheckCpu
{
    public partial class Form1 : Form
    {
        [DllImport(@"C:\Users\~User~\Projects\cpuname.dll")]
        private static extern uint IsAvxSupported();
        [DllImport(@"C:\Users\~User~\Projects\cpuname.dll")]
        private static extern IntPtr GetCpuName();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (IsAvxSupported() == 1)
                label1.Text = "AVX Support: No";
            else if (IsAvxSupported() == 2)
                label1.Text = "AVX Support: Yes";
            else
                label1.Text = "AVX Support: Unknown";

            textBox1.Text = Marshal.PtrToStringAnsi(GetCpuName());
        }
    }
}
    


Hope someone will benefit from it. I don't know if .NET already has function to check CPU brand name?


Description: Example usage calling the cpuname.DLL
Download
Filename: usecpu.ASM
Filesize: 1.12 KB
Downloaded: 279 Time(s)

Description: Assembly source file to be compiled as DLL
Download
Filename: cpuname.ASM
Filesize: 1.87 KB
Downloaded: 262 Time(s)



Last edited by FlierMate11 on 01 Feb 2023, 20:52; edited 1 time in total
Post 01 Feb 2023, 13:27
View user's profile Send private message Visit poster's website Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 1228
Location: Belarus
DimonSoft 01 Feb 2023, 15:28
I remember having nearly the same task while being a university student. We were required to write a C# program and P/Invoke a few functions from native DLL. The DLL was supposed to be written in C or C++ with Visual Studio but it somehow managed to ignore the DEF-file and decorated the exports. After spending some time trying to make the stupid toolset work as advertised, I gave up and wrote the DLL in FASM which let me do the work in about 5 minutes (as compared to something like an hour of looking for a solution with VS). Ah, good old days…
Post 01 Feb 2023, 15:28
View user's profile Send private message Visit poster's website Reply with quote
sinsi



Joined: 10 Aug 2007
Posts: 794
Location: Adelaide
sinsi 01 Feb 2023, 20:19
Off topic, but you could use PtrToStringAnsi instead of Marshal.ReadByte
Post 01 Feb 2023, 20:19
View user's profile Send private message Reply with quote
FlierMate11



Joined: 13 Oct 2022
Posts: 94
FlierMate11 01 Feb 2023, 20:55
@DimonSoft, nice hearing your story back in university project. You started coding in FASM much earlier than me. I think it is still old version of Windows at that time where DLL source code was not finalized yet by Tomasz.

@sinsi, thanks for the brilliant Marshal.PtrToStringAnsi, just one line of code. I edited the original post to reflect the changes.
Post 01 Feb 2023, 20:55
View user's profile Send private message Visit poster's website Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 1228
Location: Belarus
DimonSoft 03 Feb 2023, 12:03
FlierMate11 wrote:
I think it is still old version of Windows at that time where DLL source code was not finalized yet by Tomasz.

Not sure I got you right, or you got me right. It was FASM that solved my problem in a few minutes with very few effort. Unlike VS.
Post 03 Feb 2023, 12:03
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.