Visual Studio下C#.NET项目调用DLL报错求助
解决DLL导入引发的运行时错误(Dwmapi.dll相关)
我使用Visual Studio、C#和.NET Framework进行开发,按照教程操作至4分20秒时,测试程序出现DLL相关错误。已尝试Visual Studio提供的基础修复建议但未解决问题,以下是我的代码及错误截图:
代码片段
Form1.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Printing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Energy_Organizer { public partial class Form1 : Form { private bool drag; private int mouseX; private int mouseY; private const int WM_NCHITTEST = 0x84; private const int HTCLIENT = 0x1; private const int HTCAPTION = 0x2; private bool m_aeroEnabled; private const int CS_DROPSHADOW = 0x00020000; private const int WM_NCPAINT = 0x0085; private const int WM_ACTIVEAPP = 0x001C; [System.Runtime.InteropServices.DllImport("dwmapi.dll")] public static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMarInset); [System.Runtime.InteropServices.DllImport("dwmapi.dll")] public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize); [System.Runtime.InteropServices.DllImport("dwampi.dll")] public static extern int DwmIsCompoisionEnabled(ref int pfEnabled); [System.Runtime.InteropServices.DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")] private static extern IntPtr CreateRoundRectRgn ( int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidthEllipse, int nHeightEllipse ); public struct MARGINS { public int leftWidth; public int rightWidth; public int topHeight; public int bottomHeight; } protected override CreateParams CreateParams { get { m_aeroEnabled = CheckAeroEnabled(); CreateParams cp = base.CreateParams; if (!m_aeroEnabled) cp.ClassStyle |= CS_DROPSHADOW; return cp; } } private bool CheckAeroEnabled() { if (Environment.OSVersion.Version.Major >= 6) { int enabled = 0; DwmIsCompoisionEnabled(ref enabled); return (enabled == 1) ? true : false; } return false; } protected override void WndProc(ref Message m) { switch (m.Msg) { case WM_NCPAINT: if (m_aeroEnabled) { var v = 2; DwmSetWindowAttribute(this.Handle, 2, ref v, 4); MARGINS margins = new MARGINS() { bottomHeight = 1, leftWidth = 0, rightWidth = 0, topHeight = 0 }; DwmExtendFrameIntoClientArea(this.Handle, ref margins); } break; default: break; } base.WndProc(ref m); if (m.Msg == WM_NCHITTEST && (int)m.Result == HTCLIENT) m.Result = (IntPtr)HTCAPTION; } public Form1() { InitializeComponent(); } } }
Gradient_Sidebar_Panel.cs
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Controls; using System.Windows.Forms; namespace Energy_Organizer { internal class Gradient_Sidebar_Panel : System.Windows.Forms.Panel { public Color gradientTop {get; set;} public Color gradientBottom {get; set;} protected override void OnPaint(PaintEventArgs e) { LinearGradientBrush linear = new LinearGradientBrush(this.ClientRectangle, this.gradientTop, this.gradientBottom, 90F); Graphics graphics = e.Graphics; graphics.FillRectangle(linear, this.ClientRectangle); base.OnPaint(e); } } }
错误截图

问题修复方案
你的错误源于两处拼写错误:
- DLL文件名错误:
[System.Runtime.InteropServices.DllImport("dwampi.dll")]中,DLL名称应为dwmapi.dll(误写为dwampi),系统找不到不存在的DLL文件导致错误。 - API方法名错误:
DwmIsCompoisionEnabled方法名拼写错误,正确名称是DwmIsCompositionEnabled(单词Composition少了一个s)。
修正后的关键代码片段
// 修正DLL导入语句 [System.Runtime.InteropServices.DllImport("dwmapi.dll")] public static extern int DwmIsCompositionEnabled(ref int pfEnabled); // 修正CheckAeroEnabled方法中的调用 private bool CheckAeroEnabled() { if (Environment.OSVersion.Version.Major >= 6) { int enabled = 0; DwmIsCompositionEnabled(ref enabled); return enabled == 1; } return false; }
修复完成后,清理项目并重新生成,运行程序即可解决该DLL错误。
内容的提问来源于stack exchange,提问作者Mr Ex
相关产品推荐
相关产品推荐

