I need call a LoginUser API from MC++ dll, but when I try to call the I have
always the same exception:
"System.NullReferenceException: Object reference not set to an instance of
an object.
at LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
Int32 dwLogonType, Int32 dwLogonProvider, IntPtr phToken)"
Thanks in advance
---------------------------------
this is the sample code
//MyClass.h
#pragma once
#using <mscorlib.dll>
using namespace System;
using namespace System::Runtime::InteropServices;
namespace MyClassNameSpace
{
[DllImport( S"advapi32.dll",CharSet=CharSet::Auto, SetLastError=true)]
extern bool LogonUser( String* lpszUsername, String* lpszDomain, String*
lpszPassword,
int dwLogonType, int dwLogonProvider, IntPtr phToken);
public __gc class MyClass
{
// TODO: Add your methods for this class here.
public:
MyClass()
{
};
public:
void Login()
{
try
{
String *strUsername = new String( "username");
String *strDomain = new String( "domain");
String *strPassword = new String( "password");
IntPtr ptr = IntPtr::Zero;
bool bRet = LogonUser( strUsername, strDomain, strPassword, 3, 0, ptr);
}
catch (Exception *e)
{
System::Diagnostics::Debug::WriteLine( e);
}
};
};
}
//-------------TEST APPLICATION
//Form1.h
#pragma once
namespace test
{
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace MyClassNameSpace;
/// <summary>
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change
the
/// 'Resource File Name' property for the managed resource
compiler tool
/// associated with all .resx files this class depends on.
Otherwise,
/// the designers will not be able to interact properly with
localized
/// resources associated with this form.
/// </summary>
public __gc class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
}
protected:
void Dispose(Boolean disposing)
{
if (disposing && components)
{
components->Dispose();
}
__super::Dispose(disposing);
}
private: System::Windows::Forms::Button * button1;
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container * components;
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->button1 = new System::Windows::Forms::Button();
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(108, 92);
this->button1->Name = S"button1";
this->button1->TabIndex = 0;
this->button1->Text = S"button1";
this->button1->Click += new System::EventHandler(this, button1_Click);
//
// Form1
//
this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
this->ClientSize = System::Drawing::Size(292, 271);
this->Controls->Add(this->button1);
this->Name = S"Form1";
this->Text = S"Form1";
this->Load += new System::EventHandler(this, Form1_Load);
this->ResumeLayout(false);
}
private: System::Void button1_Click(System::Object * sender,
System::EventArgs * e)
{
MyClass *myclass = new MyClass();
myclass->Login();
}
private: System::Void Form1_Load(System::Object * sender,
System::EventArgs * e)
{
}
};
}
// Form1.cpp
#include "stdafx.h"
#include "Form1.h"
#include <windows.h>
using namespace test;
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
System::Threading::Thread::CurrentThread->ApartmentState =
System::Threading::ApartmentState::STA;
Application::Run(new Form1());
return 0;
}