Untitled
unknown
plain_text
a year ago
11 kB
6
Indexable
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Management;
namespace EvalBoard_UI
{
public partial class Form1 : Form
{
private string logDirectory = "C:\\EvalBoard_UI\\EvalBoard_UI\\LOG";
private string logFilePath;
// Hardware ID and Vendor ID for the device (can be set as per your needs)
private string hardwareId = "1268";
private string vendorId = "0112";
public Form1()
{
InitializeComponent();
CreateLog();
}
private void PCI_Connect_Click(object sender, EventArgs e)
{
WriteToLog("PCI Connect button clicked.");
// Example manual paths (provide actual paths)
string dllDriverPath = @"C:\Windows\System32\WdfCoInstaller01009.dll"; // .dll file path
// Check if DLL matches the device
if (Check_Dll_Association(dllDriverPath))
{
WriteToLog("DLL is associated with the PCI device.");
// Proceed with DLL initialization if it matches
InitializeDriver(dllDriverPath);
}
else
{
WriteToLog("The provided DLL does not match the associated device.");
}
// Now fetch the device and driver details and log them
Check_Device_Info();
}
public void CreateLog()
{
try
{
Directory.CreateDirectory(logDirectory);
logFilePath = Path.Combine(logDirectory, $"Log_{DateTime.Now:yyyyMMdd_HHmmss}.txt");
using (StreamWriter logFile = new StreamWriter(logFilePath, true))
{
logFile.WriteLine("Log created at: " + DateTime.Now);
}
}
catch (Exception ex)
{
Console.WriteLine("Error creating log file: " + ex.Message);
}
}
public void WriteToLog(string message)
{
try
{
using (StreamWriter logFile = new StreamWriter(logFilePath, true))
{
logFile.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " - " + message);
}
}
catch (Exception ex)
{
Console.WriteLine("Error writing to log: " + ex.Message);
}
}
/// <summary>
/// Initialize the driver by loading the DLL.
/// </summary>
/// <param name="dllDriverPath">Path to the .dll driver file.</param>
public void InitializeDriver(string dllDriverPath)
{
if (File.Exists(dllDriverPath) && dllDriverPath.EndsWith(".dll"))
{
WriteToLog($"Attempting to load driver DLL from: {dllDriverPath}");
// Load the DLL
IntPtr hDriver = LoadLibrary(dllDriverPath);
if (hDriver == IntPtr.Zero)
{
WriteToLog($"Failed to load driver DLL from: {dllDriverPath}. Error: {Marshal.GetLastWin32Error()}");
}
else
{
WriteToLog($"Successfully loaded driver DLL from: {dllDriverPath}");
}
}
else
{
WriteToLog("The DLL driver path is invalid or does not exist.");
}
}
// Dynamically load a DLL
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr LoadLibrary(string dllToLoad);
// Free the loaded DLL
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern bool FreeLibrary(IntPtr hModule);
// Example function for reading a PCI register (replace with actual function from your driver)
[DllImport("YourDriver.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern uint Driver_ReadRegister(uint address, out uint data);
// Example function for writing to a PCI register (replace with actual function from your driver)
[DllImport("YourDriver.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern uint Driver_WriteRegister(uint address, uint data);
/// <summary>
/// Check if the provided DLL is associated with the PCI device.
/// </summary>
/// <param name="dllDriverPath">Path to the .dll driver file to be checked.</param>
/// <returns>True if the DLL matches, otherwise false.</returns>
private bool Check_Dll_Association(string dllDriverPath)
{
try
{
// Searching for the device using ManagementObjectSearcher
ManagementObjectSearcher deviceSearcher = new ManagementObjectSearcher(
$"SELECT * FROM Win32_PnPEntity WHERE PNPDeviceID LIKE '%{vendorId}%' AND PNPDeviceID LIKE '%{hardwareId}%'");
bool deviceFound = false;
foreach (ManagementObject device in deviceSearcher.Get())
{
deviceFound = true;
// Query for the driver associated with this device
string deviceId = device["DeviceID"].ToString().Replace("\\", "\\\\");
ManagementObjectSearcher driverSearcher = new ManagementObjectSearcher(
$"SELECT * FROM Win32_PnPSignedDriver WHERE DeviceID = '{deviceId}'");
foreach (ManagementObject driver in driverSearcher.Get())
{
string associatedDriverDll = driver["DriverFile"]?.ToString();
// Log the associated DLL file path
WriteToLog($"Associated Driver DLL: {associatedDriverDll}");
// Check if the DLL matches the provided one
if (associatedDriverDll != null && associatedDriverDll.Equals(dllDriverPath, StringComparison.OrdinalIgnoreCase))
{
WriteToLog("DLL is correctly associated with the device.");
return true;
}
}
}
if (!deviceFound)
{
WriteToLog("Device not found.");
}
return false; // DLL doesn't match or device not found
}
catch (Exception ex)
{
WriteToLog($"Error while checking DLL association: {ex.Message}");
return false;
}
}
private void Check_Device_Info()
{
try
{
// Searching for the device using ManagementObjectSearcher
ManagementObjectSearcher deviceSearcher = new ManagementObjectSearcher(
$"SELECT * FROM Win32_PnPEntity WHERE PNPDeviceID LIKE '%{vendorId}%' AND PNPDeviceID LIKE '%{hardwareId}%'");
bool deviceFound = false;
using (StreamWriter logFile = new StreamWriter(logFilePath, true))
{
foreach (ManagementObject device in deviceSearcher.Get())
{
deviceFound = true;
string deviceName = (string)device["Name"];
PCI_Result.Text = deviceName;
logFile.WriteLine("Device Name: " + device["Name"]);
logFile.WriteLine("Device ID: " + device["DeviceID"]);
logFile.WriteLine("Description: " + device["Description"]);
logFile.WriteLine("Manufacturer: " + device["Manufacturer"]);
logFile.WriteLine();
// Query for the driver associated with this device
string deviceId = device["DeviceID"].ToString().Replace("\\", "\\\\");
ManagementObjectSearcher driverSearcher = new ManagementObjectSearcher(
$"SELECT * FROM Win32_PnPSignedDriver WHERE DeviceID = '{deviceId}'");
foreach (ManagementObject driver in driverSearcher.Get())
{
logFile.WriteLine("Driver Name: " + driver["DeviceName"]);
logFile.WriteLine("Driver Version: " + driver["DriverVersion"]);
logFile.WriteLine("Driver Provider: " + driver["DriverProviderName"]);
logFile.WriteLine("Driver Date: " + driver["DriverDate"]);
logFile.WriteLine();
}
}
if (!deviceFound)
{
string message = "Device not found.";
logFile.WriteLine(message);
}
}
}
catch (ManagementException e)
{
string errorMessage = "An error occurred while querying for WMI data: " + e.Message;
File.AppendAllText(logFilePath, errorMessage + Environment.NewLine);
}
catch (Exception e)
{
string errorMessage = "An unexpected error occurred: " + e.Message;
File.AppendAllText(logFilePath, errorMessage + Environment.NewLine);
}
}
private void Read_Address_Click(object sender, EventArgs e)
{
uint data;
uint status = Driver_ReadRegister(0x00, out data);
if (status == 0)
{
WriteToLog($"Read Address successful: {data}");
}
else
{
WriteToLog("Failed to read address.");
}
}
private void PCI_Write_Click(object sender, EventArgs e)
{
uint data = 0x1234;
uint status = Driver_WriteRegister(0x00, data);
if (status == 0)
{
WriteToLog("Write successful.");
}
else
{
WriteToLog("Failed to write to register.");
}
}
private void Re_init_Click(Object sender, EventArgs e)
{
// Your method implementation here
}
private void Read_PCI_Click(Object sender, EventArgs e)
{
// Your method implementation here
}
private void PCI_Connect_Result_Click(Object sender, EventArgs e)
{
// Your method implementation here
}
}
}
Editor is loading...
Leave a Comment