Problem calling Win32 API AddMonitor()

J

Jostein Leira

I'm trying to install a print monitor with a deployment project in Visual
Studio 2008, but I have problems calling theWin32 AddMonitor method.

I have included the monitor dll file in the system folder group, and the
file is present in the c:\windows\system32 folder when the AddMonitor()
method is called.

When I run the installation (msi-file), AddMonitor() always return 0. I get
the following error messages from GetLastWin32Error() dependent on what I
pass as pEnvironment and OS;

mi2.pEnvironment = "Windows NT x86"
XP:"The system could not find the environment option that was entered"
W2k3: "The system cannot find the file specified"

mi2.pEnvironment = "Windows 4.0"
w2k3 & XP: "The environment specified is invalid"

mi2.pEnvironment = null
XP: "The system cannot find message text for message number 0x%1 in the
message file for %2"
w2k3: "The system cannot find the file specified"

I suspect that there is something wrong with my PInvoke signatures due to
the error messages above, but I'm open to any suggestions.


Most of Installer1.cs:
========================
[RunInstaller(true)]
public partial class Installer1 : Installer
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct MONITOR_INFO_2
{
public string pName;
public string pEnvironment;
public string pDLLName;
}

[DllImport("winspool.drv", SetLastError = true, CharSet =
CharSet.Auto)]
private static extern Int32 AddMonitor(String pName, UInt32 Level,
ref MONITOR_INFO_2 pMonitors);

public Installer1()
{
InitializeComponent();
}

public override void Commit(IDictionary savedState)
{
base.Commit(savedState);

MONITOR_INFO_2 mi2 = new MONITOR_INFO_2();

StringBuilder buffer = new StringBuilder();
mi2.pName = "ControlPrint";
//mi2.pEnvironment = "Windows NT x86";// Orig envi, Error
XP:"The system could not find the environment option that was entered".
W2k3: "The system cannot find the file specified"
//mi2.pEnvironment = "Windows 4.0"; // Error w2k3 & XP: "The
environment specified is invalid",
mi2.pEnvironment = null; // Error XP: "The system
cannot find message text for message number 0x%1 in the message file for
%2". w2k3: "The system cannot find the file specified"
mi2.pDLLName = "wlpmon.dll";

try
{
if (AddMonitor(null, 2, ref mi2) == 0)
{
string errorMessage = new
Win32Exception(Marshal.GetLastWin32Error()).Message;
MessageBox.Show(String.Format("Adding monitor returned
false. Got Error:{0}", errorMessage), "Install point", MessageBoxButtons.OK,
MessageBoxIcon.Error);

return;
}
else
MessageBox.Show("Commiting ControlPrint
installation\n\nMonitor added successfully.", "Install point",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception e)
{
MessageBox.Show(String.Format("Got excepiton:\n\n{0}",
e.ToString()), "Install point", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}

========================

Best regards



Jostein Leira
Innoventure Net Solutions AS
 
L

Linda Liu[MSFT]

Hi Jostein,

First of all, the caller of the AddMonitor function must have the
"SeLoadDriverPrivilege".

FYI, you may check the user rights in the "Policy" column of the "User
Rights Assignment" node of the Local Security Settings Microsoft Management
Console(MMC) snap-in. The operation system represents the privilege of
"SeLoadDriverPrivilege" with the string "Load and unload device drivers".

IMO, the PInvoke signatures of the AddMonitor function in your sample code
should be correct. And the pEnvironment field of the passed MONITOR_INFO_2
parameter should have the value of null.

I suggest that you create a new WinForm application project and call the
AddMinitor function in the new application to see if you can install a
local port monitor successfully.

If you need our further assistance, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jostein Leira

Thanks for your reply. While trying your suggestions I found that my problem
was due to an error that occurred in the monitor dll. So the code in the
original post seems to work.

Best regards



Jostein Leira
Innoventure Net Solutions AS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top