B
Brian Stoop
..NET 1.1
I have a long running program that runs various methods from system DLL's.
I've pasted some calls below.
My program is leaking memory. Could the leak be due to these calls, or am I
looking in the wrong place ?
thanks, Brian
//
// Struct and DLLImports
//
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public struct USER_INFO_1
{
public string usri1_name;
public string usri1_password;
public int usri1_password_age;
public int usri1_priv;
public string usri1_home_dir;
public string comment;
public int usri1_flags;
public string usri1_script_path;
}
[DllImport("Netapi32.dll")]
extern static int NetLocalGroupDelMembers(
[MarshalAs(UnmanagedType.LPWStr)] string servername,
[MarshalAs(UnmanagedType.LPWStr)] string groupname,
int level,
ref LOCALGROUP_MEMBERS_INFO_3 buf,
int entries);
[DllImport("kernel32.dll",
CharSet=System.Runtime.InteropServices.CharSet.Auto)]
private unsafe static extern int FormatMessage(
int dwFlags,
ref IntPtr lpSource,
int dwMessageId,
int dwLanguageId,
ref String lpBuffer,
int nSize,
IntPtr *Arguments);
//
// Methods and calls
//
private unsafe static string GetErrorMessage(int errorCode)
{
int FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100;
int FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200;
int FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000;
int messageSize = 255;
String lpMsgBuf = "";
int dwFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_IGNORE_INSERTS;
IntPtr ptrlpSource = IntPtr.Zero;
IntPtr prtArguments = IntPtr.Zero;
int retVal = FormatMessage(dwFlags, ref ptrlpSource, errorCode, 0, ref
lpMsgBuf, messageSize, &prtArguments);
if (0 == retVal)
{
throw new Exception("Failed to format message for error code " +
errorCode + ". ");
}
// Clean any newlines and line feeds
lpMsgBuf=lpMsgBuf.Replace("\r", "");
lpMsgBuf=lpMsgBuf.Replace("\n", "");
return lpMsgBuf;
}
// Create user fred on buck10
USER_INFO_1 UserInfo1 = new USER_INFO_1();
UserInfo1.usri1_name = "FRED";
UserInfo1.usri1_password = "password";
UserInfo1.usri1_priv = 1;
UserInfo1.usri1_home_dir = null;
UserInfo1.comment = "Freds account";
UserInfo1.usri1_script_path = null;
const int UF_DONT_EXPIRE_PASSWD = 65536;
UserInfo1.usri1_flags=UF_DONT_EXPIRE_PASSWD;
int ret = ret=NetUserAdd("buck10", 1, ref UserInfo1, 0);
I have a long running program that runs various methods from system DLL's.
I've pasted some calls below.
My program is leaking memory. Could the leak be due to these calls, or am I
looking in the wrong place ?
thanks, Brian
//
// Struct and DLLImports
//
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public struct USER_INFO_1
{
public string usri1_name;
public string usri1_password;
public int usri1_password_age;
public int usri1_priv;
public string usri1_home_dir;
public string comment;
public int usri1_flags;
public string usri1_script_path;
}
[DllImport("Netapi32.dll")]
extern static int NetLocalGroupDelMembers(
[MarshalAs(UnmanagedType.LPWStr)] string servername,
[MarshalAs(UnmanagedType.LPWStr)] string groupname,
int level,
ref LOCALGROUP_MEMBERS_INFO_3 buf,
int entries);
[DllImport("kernel32.dll",
CharSet=System.Runtime.InteropServices.CharSet.Auto)]
private unsafe static extern int FormatMessage(
int dwFlags,
ref IntPtr lpSource,
int dwMessageId,
int dwLanguageId,
ref String lpBuffer,
int nSize,
IntPtr *Arguments);
//
// Methods and calls
//
private unsafe static string GetErrorMessage(int errorCode)
{
int FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100;
int FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200;
int FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000;
int messageSize = 255;
String lpMsgBuf = "";
int dwFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_IGNORE_INSERTS;
IntPtr ptrlpSource = IntPtr.Zero;
IntPtr prtArguments = IntPtr.Zero;
int retVal = FormatMessage(dwFlags, ref ptrlpSource, errorCode, 0, ref
lpMsgBuf, messageSize, &prtArguments);
if (0 == retVal)
{
throw new Exception("Failed to format message for error code " +
errorCode + ". ");
}
// Clean any newlines and line feeds
lpMsgBuf=lpMsgBuf.Replace("\r", "");
lpMsgBuf=lpMsgBuf.Replace("\n", "");
return lpMsgBuf;
}
// Create user fred on buck10
USER_INFO_1 UserInfo1 = new USER_INFO_1();
UserInfo1.usri1_name = "FRED";
UserInfo1.usri1_password = "password";
UserInfo1.usri1_priv = 1;
UserInfo1.usri1_home_dir = null;
UserInfo1.comment = "Freds account";
UserInfo1.usri1_script_path = null;
const int UF_DONT_EXPIRE_PASSWD = 65536;
UserInfo1.usri1_flags=UF_DONT_EXPIRE_PASSWD;
int ret = ret=NetUserAdd("buck10", 1, ref UserInfo1, 0);