Please Help DLL Issue

B

ba.hons

Hi all,

Am wondering if anyone can help.

I have received a C++ none COM DLL that i must use to interact to a
peice of hardware. As I am more confident in C# I decided to create a
wrapper for the DLL. As i have found out this is not possible for MS
visual studio to do this automatically from a COM object i created a
C# DLL project to act as a wrapper for the C++ DLL.

Unfortunetly i was quite silly and created all methods in the wrapper
before testing that i could create an instance of my wrapper and the
dll would work.

Now the weird thing is lets say i have a method called init() in the C+
+ code, i have created a method like this in my c# code


public void Init()
{
//set to -1 so that we dont return succuss message by default
int x=-1;

try
{
Process p = Process.GetCurrentProcess();

IntPtr HWND = p.MainWindowHandle;
x = MyWrapper.Initialization();

if(x==0)
{
System.Console.WriteLine(x.ToString());
}
else
{
System.Console.WriteLine(x.ToString());
throw new Exception(x.ToString());
}
}
//an exception occured so return appropriate error message
catch(Exception ex)
{
System.Console.WriteLine(x.ToString());
throw new Exception(ex.Message);
}
}

Now these methods sometimes seem to work in my test application were i
create an instance of my wrapper and call the methods like this:

MYWrapper test = new MyWrapper();

test.MYInitialization()

BUT sometimes they dont seem to work, also i have noticed that if i
add the DLL imports to the top of my test application like this:

[DllImport("Vertx.dll", SetLastError = true, CharSet =
CharSet.Ansi)]
static extern int Initialization(int Port, int HWND, int HINSTANCE);

the methods seem to work more often? even though i never actually call
this method!

Am sorry if i have jumped around a bit but i think i have burnt out my
brain trying to understand what is wrong here.

Also the parameters for the init method in the C++ DLL are:

int Initialization ( int port , HWND hWnd , HINSTANCE hInst )
and i am using:

4070, HWND.ToInt32(),moduleHandle.ToInt32()

in the C# code, am not sure these are correct equivilents.

Any help would be REALLY apprecaited!

Thanks,

Adam
 
N

Nicholas Paldino [.NET/C# MVP]

Well, the first thing to figure out is, are you trying to use classes,
or functions that are exported from the dll? If you are just using
functions that are exported, then all you have to do is make the function
calls through the P/Invoke layer.

However, it seems that you need to use classes that are in the DLL. In
this case, you need to create a managed C++ project which will create a
managed assembly, but will also allow you to create the unmanaged types you
are trying to access.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

ba.hons said:
Hi all,

Am wondering if anyone can help.

I have received a C++ none COM DLL that i must use to interact to a
peice of hardware. As I am more confident in C# I decided to create a
wrapper for the DLL. As i have found out this is not possible for MS
visual studio to do this automatically from a COM object i created a
C# DLL project to act as a wrapper for the C++ DLL.

Unfortunetly i was quite silly and created all methods in the wrapper
before testing that i could create an instance of my wrapper and the
dll would work.

Now the weird thing is lets say i have a method called init() in the C+
+ code, i have created a method like this in my c# code


public void Init()
{
//set to -1 so that we dont return succuss message by default
int x=-1;

try
{
Process p = Process.GetCurrentProcess();

IntPtr HWND = p.MainWindowHandle;
x = MyWrapper.Initialization();

if(x==0)
{
System.Console.WriteLine(x.ToString());
}
else
{
System.Console.WriteLine(x.ToString());
throw new Exception(x.ToString());
}
}
//an exception occured so return appropriate error message
catch(Exception ex)
{
System.Console.WriteLine(x.ToString());
throw new Exception(ex.Message);
}
}

Now these methods sometimes seem to work in my test application were i
create an instance of my wrapper and call the methods like this:

MYWrapper test = new MyWrapper();

test.MYInitialization()

BUT sometimes they dont seem to work, also i have noticed that if i
add the DLL imports to the top of my test application like this:

[DllImport("Vertx.dll", SetLastError = true, CharSet =
CharSet.Ansi)]
static extern int Initialization(int Port, int HWND, int HINSTANCE);

the methods seem to work more often? even though i never actually call
this method!

Am sorry if i have jumped around a bit but i think i have burnt out my
brain trying to understand what is wrong here.

Also the parameters for the init method in the C++ DLL are:

int Initialization ( int port , HWND hWnd , HINSTANCE hInst )
and i am using:

4070, HWND.ToInt32(),moduleHandle.ToInt32()

in the C# code, am not sure these are correct equivilents.

Any help would be REALLY apprecaited!

Thanks,

Adam
 

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