By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,066 Members | 1,841 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,066 IT Pros & Developers. It's quick & easy.

Using iscsidsc.dll in Csharp

P: n/a
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,0xFFFFFFFFFFFF FFFF,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
Nov 19 '08 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.