Using iscsidsc.dll in Csharp

J

jimmy.dansbo

Hi,

I am trying to use iscsidsc.dll in a csharp program, but have a lot of
trouble with it.
One thing I have working is GetIScsiInitiatorNodeName in the following
way:

[DLLImport("iscsidsc.dll")]
public static extern Int32 GetIScsiInitiatorNodeName(StringBuilder
InitiatorNodeName);

StringBuilder NodeName = new StringBuilder(256);
GetIScsiInitiatorNodeName(NodeName);
textBox1.Text = NodeName.ToString();

The next one I was trying to get to work is AddIScsiSendTargetPortal,
but it requires the passing of pointers to structures.
Following is the information from MSDN:
HRESULT WINAPI AddIScsiSendTargetPortal(
__in_opt PTCHAR InitiatorName,
__in_opt ULONG InitiatorPortNumber,
__in_opt PISCI_LOGIN_OPTIONS LoginOptions,
__in_opt ISCSI_SECURITY_FLAGS SecurityFlags,
PISCSI_TARGET_PORTAL Portal
);

I have created it in c# as follows:
[DLLImport("scsidsc.dll")]
public static extern Int32 AddIScsiSendTargetPortal(
StringBuilder,
ulong InitiatorPortNumber,
ref ISCSI_LOGIN_OPTIONS LoginOptions,
UInt64 SecurityFlags,
ref ISCSI_TARGET_PORTAL Portal
);

Structures are as follows:
[StructLayout(LayoutKind.Sequential)]
public struct ISCSI_LOGIN_OPTIONS {
public ulong Version;
public uint InformationSpecified;
public uint LoginFlags;
public uint AuthType;
public byte HeaderDigest;
public byte DataDigest;
public ulong MaximumConnections;
public ulong DefaultTime2Wait;
public ulong DefaultTime2Retain;
public ulong UsernameLength;
public ulong PasswordLength;
[MarshalAs(UnmanagedType.LPTStr)]
public string Username;
[MarshalAs(UnmanagedType.LPTStr)]
public string password;
}

[StructLayout(LayoutKind.Sequential)]
public struct ISCSI_TARGET_PORTAL {
[MarshalAs(UnamanagedType.ByValTStr, SizeConst=256)]
public string SymbolicName;
[MarshalAs(UnamanagedType.ByValTStr, SizeConst=256)]
public string Address;
public ushort Socket
}

When trying to call AddIScsiSendTargetPortal I do it as follows:
ISCSI_TARGET_PORTAL Portal = new ISCSI_TARGET_PORTAL();
Portal.Address = new string(new char[256]);
Portal.SymbolicName = new string(new char[256]);
ISCSI_LOGIN_OPTIONS LoginOptions = new ISCSI_LOGIN_OPTIONS();
LoginOptions.Username = new string(new char[32]);
LoginOptions.Password = new string(new char[32]);
LoginOptions.UsernameLength=32;
LoginOptions.PasswordLength=32;

AddIScsiSendTargetPortal("localhost,0xFFFFFFFFFFFFFFFF,ref
LoginOptions, 0, ref Portal);

The code compiles, but when run I get the following error:
Attempted to read or write protected memory. This is often an
indication that other memory is corrupt.

I have tried several different ways of defining the function and the
structures, but so far without any luck.
I would really like some help on how to get this working.
Thanks in advance
Regards
Jimmy Dansbo
 

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