Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DLL library cannot be called in VB VB or C# #2

Open
zyyujq opened this issue Feb 12, 2023 · 7 comments
Open

DLL library cannot be called in VB VB or C# #2

zyyujq opened this issue Feb 12, 2023 · 7 comments

Comments

@zyyujq
Copy link

zyyujq commented Feb 12, 2023

DLL library cannot be called in VB or C#,
The DLL library version has been V8.17.14,
How to modify to__ Stdcall, and call demo program

@PeratX
Copy link
Member

PeratX commented Feb 12, 2023

using System;
using System.Runtime.InteropServices;

namespace PXDriveTester.Component
{
    class CDIEmbedded
    {
        public enum InterfaceType
        {
            InterfaceTypeUnknown = 0,
            InterfaceTypePata,
            InterfaceTypeSata,
            InterfaceTypeUsb,
            InterfaceTypeIeee1394,
            //	INTERFACE_TYPE_UASP,
            InterfaceTypeScsi,
            InterfaceTypeNvme,
            //	INTERFACE_TYPE_USB_NVME,
        };

        public struct SmartAttribute
        {
            public byte Id;
            public int StatusFlags;
            public byte CurrentValue;
            public byte WorstValue;
            public long RawValue;
            public byte Reserved;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct RawSmart
        {
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)]
            public byte[] raw;
        }

        [DllImport("cdie.dll")]
        public static extern IntPtr CreateAtaSmart();

        [DllImport("cdie.dll")]
        public static extern void DestroyAtaSmart(IntPtr a);

        [DllImport("cdie.dll")]
        public static extern void InitAtaSmart(IntPtr a, bool useWmi, bool advancedDiskSearch, bool flagChangeDisk,
            bool workaroundHD204UI, bool workaroundAdataSsd, bool flagHideNoSmartDisk);

        [DllImport("cdie.dll")]
        public static extern int GetDiskCount(IntPtr a);

        [DllImport("cdie.dll")]
        public static extern int GetPhysicalDriveId(IntPtr a, int index);

        [DllImport("cdie.dll")]
        [return: MarshalAs(UnmanagedType.LPStr)]
        public static extern string GetModel(IntPtr a, int index);

        [DllImport("cdie.dll")]
        [return: MarshalAs(UnmanagedType.LPStr)]
        public static extern string GetSerialNumber(IntPtr a, int index);

        [DllImport("cdie.dll")]
        [return: MarshalAs(UnmanagedType.LPStr)]
        public static extern string GetFirmware(IntPtr a, int index);

        [DllImport("cdie.dll")]
        public static extern IntPtr GetSmart(IntPtr a, int index);
        
        [DllImport("cdie.dll")]
        public static extern int GetInterfaceType(IntPtr a, int index);
        
        [DllImport("cdie.dll")]
        public static extern uint GetTotalDiskSize(IntPtr a, int index);

        [DllImport("cdie.dll")]
        public static extern bool CreateRandomFile(string path, int size);

        public static SmartAttribute[] ReadSmart(IntPtr a, int index)
        {
            var ptr = GetSmart(a, index);
            var smart = new SmartAttribute[30];
            for (var i = 0; i < 30; i++)
            {
                var raw = Marshal.PtrToStructure<RawSmart>(ptr);
                smart[i].Id = raw.raw[0];
                smart[i].StatusFlags = raw.raw[2] << 8 + raw.raw[1];
                smart[i].CurrentValue = raw.raw[3];
                smart[i].WorstValue = raw.raw[4];
                smart[i].RawValue = RawValueToLong(raw.raw);
                smart[i].Reserved = raw.raw[11];
                ptr += 12;
            }

            return smart;
        }

        private static long RawValueToLong(byte[] buf)
        {
            return (Convert.ToInt64(buf[10] & 0xff) << 40) +
                   (Convert.ToInt64(buf[9] & 0xff) << 32) +
                   ((buf[8] & 0xff) << 24) +
                   ((buf[7] & 0xff) << 16) +
                   ((buf[6] & 0xff) << 8) +
                   (buf[5] & 0xff);
        }
    }
}

@zyyujq
Copy link
Author

zyyujq commented Feb 12, 2023

Thank you for your answer!
As a beginner of C++programming, I don't quite understand.

The CDIEmbedded class has been created. How can I call it in the window?

in VC++:
#include "AtaSmart.h"

CAtaSmart m_ATA{};
m_ATA.Init(1, 1, 0, 1, 1, 0);
CString HDDID=CT2A(m_ATA.vars[0].ModelSerial);

in C# :
How to call this CDIEmbedded class in the C # form?
How to simplify the CrystalDiskInfoEmbedded library to easily obtain the disk model or serial number in C #?

@zyyujq
Copy link
Author

zyyujq commented Feb 12, 2023

class CDIEmbedded

C# code in Windows11
IntPtr p= CDIEmbedded.CreateAtaSmart() ;
CDIEmbedded.InitAtaSmart(p, true, true, false, true, true, false);
textBox1.Text = CDIEmbedded.GetModel(p, 0).ToString();

report errors : External component exception

@zyyujq
Copy link
Author

zyyujq commented Feb 12, 2023

VB.NET 2017
CrystalDiskInfoEmbedded =v8.9
Windows 7:
Test passed
Windows 11:
Test failed

code:
Imports System.Runtime.InteropServices

Public Class Form1

#Region
#If x86 Then
Const ImDllStr As String = "X86\CrystalDiskInfoEmbedded.dll"
#Else
Const ImDllStr As String = "X64\CrystalDiskInfoEmbedded.dll"
#End If

#End Region

<DllImport(ImDllStr, CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)>
Public Shared Function CreateAtaSmart() As IntPtr
End Function

<DllImport(ImDllStr, CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)>
Public Shared Sub DestroyAtaSmart(ByVal ptr As IntPtr)
End Sub


<DllImport(ImDllStr, CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)>
Public Shared Sub InitAtaSmart(ByVal ptr As IntPtr, ByVal useWmi As Boolean, ByVal advancedDiskSearch As Boolean, ByVal flagChangeDisk As Boolean, ByVal workaroundHD204UI As Boolean, ByVal workaroundAdataSsd As Boolean, ByVal flagHideNoSmartDisk As Boolean)
End Sub


<DllImport(ImDllStr)>
Public Shared Function GetPhysicalDriveId(ByVal ptr As IntPtr, ByVal index As Integer) As Integer
End Function

<DllImport(ImDllStr)>
Public Shared Function GetInterfaceType(ByVal ptr As IntPtr, ByVal index As Integer) As Integer
End Function

<DllImport(ImDllStr)>
Public Shared Function GetModel(ByVal ptr As IntPtr, ByVal index As Integer) As String
End Function

<DllImport(ImDllStr)>
Public Shared Function GetSerialNumber(ByVal ptr As IntPtr, ByVal index As Integer) As String
End Function

<DllImport(ImDllStr)>
Public Shared Function GetFirmware(ByVal ptr As IntPtr, ByVal index As Integer) As String
End Function

<DllImport(ImDllStr)>
Public Shared Function GetSmart(ByVal ptr As IntPtr, ByVal index As Integer) As IntPtr
End Function

<DllImport(ImDllStr)>
Public Shared Function GetTotalDiskSize(ByVal ptr As IntPtr, ByVal index As Integer) As UInteger
End Function

<DllImport(ImDllStr)>
Public Shared Function GetDiskCount(ByVal ptr As IntPtr) As Integer
End Function

<DllImport(ImDllStr)>
Public Shared Function CreateRandomFile(ByVal path As String, ByVal size As Integer) As Boolean
End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim P As IntPtr = CreateAtaSmart()
    InitAtaSmart(P, 1, 0, 0, 1, 1, 0)
    TextBox1.Text = GetModel(P, 0).ToString
    TextBox2.Text = GetSerialNumber(P, 0).ToString
    DestroyAtaSmart(P)
End Sub

End Class

@zyyujq
Copy link
Author

zyyujq commented Feb 12, 2023

Deepens the learning and application of pointer parameters

VC++:
void DestroyAtaSmart(CAtaSmart* ptr)

VB.NET:
<DllImport(ImDllStr)>
Public Shared Sub DestroyAtaSmart(ByVal ptr As IntPtr)
End Sub

C#:
[DllImport(ImDllStr)]
public static extern void DestroyAtaSmart(IntPtr a);

@zyyujq
Copy link
Author

zyyujq commented Feb 13, 2023

Save the following content as APP.exe.manifest.
In the input and output of the list tool on the VS2019 project property page, add the additional list file setting to APP.exe.manifest.


APP.exe.manifest:

< ?xml version="1.0" encoding="UTF-8" standalone="yes"?>
< assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
< compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
< application>
< !-- Windows 10 and Windows 11 -->
< supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>
< !-- Windows 8.1 -->
< supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>
< !-- Windows 8 -->
< supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/>
< !-- Windows 7 -->
< supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>
< !-- Windows Vista -->
< supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/>
< /application>
< /compatibility>
< /assembly>

@PeratX
Copy link
Member

PeratX commented Feb 15, 2023

you should export the functions as I do. cdelspec or stdcall depends on your application

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants