c# wrapper updates, etc.

This commit is contained in:
Jim Norman
2006-08-18 17:41:08 +00:00
parent 169e3a389d
commit 2ec99203a2
7 changed files with 98 additions and 93 deletions

View File

@@ -10,23 +10,21 @@ namespace Novell.Casa.Client.Auth
public class Authtoken
{
private const string AUTH_LIBRARY = "C:\\Program Files\\Novell\\CASA\\lib\\authtoken";
private const int BUFFER_OVERFLOW = 6;
[DllImport(AUTH_LIBRARY, CharSet=CharSet.None) ]
public static extern int ObtainAuthToken
(
[In] byte[] baService,
[In] byte[] baHost,
IntPtr pToken,
[In, Out] ref int iTokenLength
[In, Out] byte[] baToken,
[In, Out] ref int iTokenLength
);
public Authtoken()
{
//
// TODO: Add constructor logic here
//
}
@@ -34,10 +32,8 @@ namespace Novell.Casa.Client.Auth
{
int rcode = 0;
byte[] baService = null;
byte[] baHost = null;
int bufferSize = 4096;
IntPtr pBuffer = Marshal.AllocHGlobal(bufferSize);
byte[] baHost = null;
int bufferSize = 0;
byte[] baToken = new byte[bufferSize];
@@ -56,45 +52,45 @@ namespace Novell.Casa.Client.Auth
{
baHost = Encoding.ASCII.GetBytes(sHost);
}
LogMessage("Calling Native API->ObtainAuthToken");
LogMessage("Buffer size is "+ bufferSize);
else
{
throw new Exception("Invalid parameter");
}
// call with buffersize of 0. This way we determine the exact size.
try
{
rcode = ObtainAuthToken(baService, baHost, pBuffer, ref bufferSize);
rcode = ObtainAuthToken(baService, baHost, baToken, ref bufferSize);
int test = (rcode & BUFFER_OVERFLOW);
if (test == BUFFER_OVERFLOW)
{
// now allocate the proper size
baToken = new byte[bufferSize];
rcode = ObtainAuthToken(baService, baHost, baToken, ref bufferSize);
}
}
catch (Exception e)
{
LogMessage(e.ToString());
return null;
}
LogMessage("ObtainAuthToken returned " + rcode);
if (rcode != 0)
{
Marshal.FreeHGlobal(pBuffer);
{
throw new Exception(rcode.ToString());
}
else
{
// marshal the IntPtr into a byte array
String sTemp = Marshal.PtrToStringAnsi(pBuffer, bufferSize);
Marshal.FreeHGlobal(pBuffer);
return (Encoding.ASCII.GetBytes(sTemp));
}
return null;
return baToken;
}
}
private static void LogMessage(string sMessage)
{
System.Diagnostics.Trace.WriteLine("(C#)AuthToken: " + sMessage);
}
}
}