DllImportAttribute CallingConvention

M

Mr.Tickle

I am calling an unmanaged DLL like follows in a Class wrapper.

namespace blah
{
[DllImport("DLLName.DLL", EntryPoint="BlahEntryPoint",
CallingConvention=CallingConvention.ThisCall]
public static extern void blahFn(out long blah, out long blah2);

class sealed ClassBlah
{
private ClassBlah()
{
}

}
}

Why have I to call this with the CallingConvention of ThisCall, its just
normal "C" functions in a DLL. Why wont StdCall work?

What happens with other calling conventions is that the out parameters are
NOT set, yet with ThisCall, they are set ok, or appear to be. I find it odd.



Thanks
 
N

Nicholas Paldino [.NET/C# MVP]

Mr. Tickle,

What is the declaration of the original function? If it takes a
pointer, then you should be using "ref" for the parameters, and not out.
Also, does the function take a 64-bit integer? Long in C is a 32-bit
integer, where in .NET it is a 64-bit integer.

The STDCALL mechanism should work if you are using it.

Hope this helps.
 
M

Mr.Tickle

Nop, I tried ref and it doesnt help, the only way i can get it to return
values is with thiscall.



Nicholas Paldino said:
Mr. Tickle,

What is the declaration of the original function? If it takes a
pointer, then you should be using "ref" for the parameters, and not out.
Also, does the function take a 64-bit integer? Long in C is a 32-bit
integer, where in .NET it is a 64-bit integer.

The STDCALL mechanism should work if you are using it.

Hope this helps.


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

Mr.Tickle said:
I am calling an unmanaged DLL like follows in a Class wrapper.

namespace blah
{
[DllImport("DLLName.DLL", EntryPoint="BlahEntryPoint",
CallingConvention=CallingConvention.ThisCall]
public static extern void blahFn(out long blah, out long blah2);

class sealed ClassBlah
{
private ClassBlah()
{
}

}
}

Why have I to call this with the CallingConvention of ThisCall, its just
normal "C" functions in a DLL. Why wont StdCall work?

What happens with other calling conventions is that the out parameters are
NOT set, yet with ThisCall, they are set ok, or appear to be. I find it odd.



Thanks
 
M

Mr.Tickle

So i should use Int32 for the parameters instead of long?


Nicholas Paldino said:
Mr. Tickle,

What is the declaration of the original function? If it takes a
pointer, then you should be using "ref" for the parameters, and not out.
Also, does the function take a 64-bit integer? Long in C is a 32-bit
integer, where in .NET it is a 64-bit integer.

The STDCALL mechanism should work if you are using it.

Hope this helps.


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

Mr.Tickle said:
I am calling an unmanaged DLL like follows in a Class wrapper.

namespace blah
{
[DllImport("DLLName.DLL", EntryPoint="BlahEntryPoint",
CallingConvention=CallingConvention.ThisCall]
public static extern void blahFn(out long blah, out long blah2);

class sealed ClassBlah
{
private ClassBlah()
{
}

}
}

Why have I to call this with the CallingConvention of ThisCall, its just
normal "C" functions in a DLL. Why wont StdCall work?

What happens with other calling conventions is that the out parameters are
NOT set, yet with ThisCall, they are set ok, or appear to be. I find it odd.



Thanks
 
M

Mr.Tickle

I also have a problem with "ref" paramaters, it says they need to be
assigned to before used

Yet i have them used as "ref" in dll extern calls and checked for non NULL
after yet it complains for non assignment so i use a local copy.

Isnt it smart enough to detect that its being assinged in an extern dll call
as declared in DllImport?




Nicholas Paldino said:
Mr. Tickle,

What is the declaration of the original function? If it takes a
pointer, then you should be using "ref" for the parameters, and not out.
Also, does the function take a 64-bit integer? Long in C is a 32-bit
integer, where in .NET it is a 64-bit integer.

The STDCALL mechanism should work if you are using it.

Hope this helps.


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

Mr.Tickle said:
I am calling an unmanaged DLL like follows in a Class wrapper.

namespace blah
{
[DllImport("DLLName.DLL", EntryPoint="BlahEntryPoint",
CallingConvention=CallingConvention.ThisCall]
public static extern void blahFn(out long blah, out long blah2);

class sealed ClassBlah
{
private ClassBlah()
{
}

}
}

Why have I to call this with the CallingConvention of ThisCall, its just
normal "C" functions in a DLL. Why wont StdCall work?

What happens with other calling conventions is that the out parameters are
NOT set, yet with ThisCall, they are set ok, or appear to be. I find it odd.



Thanks
 
M

Mr.Tickle

i mean "out" oops.

ref in the same dll wrapper still gets the output but only with thiscall
convention.


Mr.Tickle said:
I also have a problem with "ref" paramaters, it says they need to be
assigned to before used

Yet i have them used as "ref" in dll extern calls and checked for non NULL
after yet it complains for non assignment so i use a local copy.

Isnt it smart enough to detect that its being assinged in an extern dll call
as declared in DllImport?




in message news:#[email protected]...
Mr. Tickle,

What is the declaration of the original function? If it takes a
pointer, then you should be using "ref" for the parameters, and not out.
Also, does the function take a 64-bit integer? Long in C is a 32-bit
integer, where in .NET it is a 64-bit integer.

The STDCALL mechanism should work if you are using it.

Hope this helps.


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

Mr.Tickle said:
I am calling an unmanaged DLL like follows in a Class wrapper.

namespace blah
{
[DllImport("DLLName.DLL", EntryPoint="BlahEntryPoint",
CallingConvention=CallingConvention.ThisCall]
public static extern void blahFn(out long blah, out long blah2);

class sealed ClassBlah
{
private ClassBlah()
{
}

}
}

Why have I to call this with the CallingConvention of ThisCall, its just
normal "C" functions in a DLL. Why wont StdCall work?

What happens with other calling conventions is that the out parameters are
NOT set, yet with ThisCall, they are set ok, or appear to be. I find
it
odd.
 
N

Nicholas Paldino [.NET/C# MVP]

If you could post the original function declaration, it would help
tremendously.


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

Mr.Tickle said:
i mean "out" oops.

ref in the same dll wrapper still gets the output but only with thiscall
convention.


Mr.Tickle said:
I also have a problem with "ref" paramaters, it says they need to be
assigned to before used

Yet i have them used as "ref" in dll extern calls and checked for non NULL
after yet it complains for non assignment so i use a local copy.

Isnt it smart enough to detect that its being assinged in an extern dll call
as declared in DllImport?




in message news:#[email protected]...
Mr. Tickle,

What is the declaration of the original function? If it takes a
pointer, then you should be using "ref" for the parameters, and not out.
Also, does the function take a 64-bit integer? Long in C is a 32-bit
integer, where in .NET it is a 64-bit integer.

The STDCALL mechanism should work if you are using it.

Hope this helps.


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

I am calling an unmanaged DLL like follows in a Class wrapper.

namespace blah
{
[DllImport("DLLName.DLL", EntryPoint="BlahEntryPoint",
CallingConvention=CallingConvention.ThisCall]
public static extern void blahFn(out long blah, out long blah2);

class sealed ClassBlah
{
private ClassBlah()
{
}

}
}

Why have I to call this with the CallingConvention of ThisCall, its just
normal "C" functions in a DLL. Why wont StdCall work?

What happens with other calling conventions is that the out
parameters
are
NOT set, yet with ThisCall, they are set ok, or appear to be. I find it
odd.



Thanks
 
M

Mr.Tickle

It would but I dont have it, its in a dll i have to call into. Not mine.




Nicholas Paldino said:
If you could post the original function declaration, it would help
tremendously.


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

Mr.Tickle said:
i mean "out" oops.

ref in the same dll wrapper still gets the output but only with thiscall
convention.


Mr.Tickle said:
I also have a problem with "ref" paramaters, it says they need to be
assigned to before used

Yet i have them used as "ref" in dll extern calls and checked for non NULL
after yet it complains for non assignment so i use a local copy.

Isnt it smart enough to detect that its being assinged in an extern
dll
call
as declared in DllImport?




in message Mr. Tickle,

What is the declaration of the original function? If it takes a
pointer, then you should be using "ref" for the parameters, and not out.
Also, does the function take a 64-bit integer? Long in C is a 32-bit
integer, where in .NET it is a 64-bit integer.

The STDCALL mechanism should work if you are using it.

Hope this helps.


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

I am calling an unmanaged DLL like follows in a Class wrapper.

namespace blah
{
[DllImport("DLLName.DLL", EntryPoint="BlahEntryPoint",
CallingConvention=CallingConvention.ThisCall]
public static extern void blahFn(out long blah, out long blah2);

class sealed ClassBlah
{
private ClassBlah()
{
}

}
}

Why have I to call this with the CallingConvention of ThisCall, its
just
normal "C" functions in a DLL. Why wont StdCall work?

What happens with other calling conventions is that the out parameters
are
NOT set, yet with ThisCall, they are set ok, or appear to be. I
find
it
odd.



Thanks
 
M

Mr.Tickle

typedef SOME_ID long

SOME_ID someFn(SOME_ID* someIDhere, SOME_ID* someIDhere2, SOME_ID*
someIDhere3);

something like that.


Nicholas Paldino said:
If you could post the original function declaration, it would help
tremendously.


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

Mr.Tickle said:
i mean "out" oops.

ref in the same dll wrapper still gets the output but only with thiscall
convention.


Mr.Tickle said:
I also have a problem with "ref" paramaters, it says they need to be
assigned to before used

Yet i have them used as "ref" in dll extern calls and checked for non NULL
after yet it complains for non assignment so i use a local copy.

Isnt it smart enough to detect that its being assinged in an extern
dll
call
as declared in DllImport?




in message Mr. Tickle,

What is the declaration of the original function? If it takes a
pointer, then you should be using "ref" for the parameters, and not out.
Also, does the function take a 64-bit integer? Long in C is a 32-bit
integer, where in .NET it is a 64-bit integer.

The STDCALL mechanism should work if you are using it.

Hope this helps.


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

I am calling an unmanaged DLL like follows in a Class wrapper.

namespace blah
{
[DllImport("DLLName.DLL", EntryPoint="BlahEntryPoint",
CallingConvention=CallingConvention.ThisCall]
public static extern void blahFn(out long blah, out long blah2);

class sealed ClassBlah
{
private ClassBlah()
{
}

}
}

Why have I to call this with the CallingConvention of ThisCall, its
just
normal "C" functions in a DLL. Why wont StdCall work?

What happens with other calling conventions is that the out parameters
are
NOT set, yet with ThisCall, they are set ok, or appear to be. I
find
it
odd.



Thanks
 
N

Nicholas Paldino [.NET/C# MVP]

Mr.Tickle,

In this case, you definitely want to use the int type, and pass it by
reference. Your declaration should look like this:

[DllImport("somedll.dll")]
public static extern int someFn(ref int someIDhere, ref int someIDhere2, ref
int someIDhere3);


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

Mr.Tickle said:
typedef SOME_ID long

SOME_ID someFn(SOME_ID* someIDhere, SOME_ID* someIDhere2, SOME_ID*
someIDhere3);

something like that.


in message news:[email protected]...
If you could post the original function declaration, it would help
tremendously.


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

Mr.Tickle said:
i mean "out" oops.

ref in the same dll wrapper still gets the output but only with thiscall
convention.


I also have a problem with "ref" paramaters, it says they need to be
assigned to before used

Yet i have them used as "ref" in dll extern calls and checked for
non
NULL
after yet it complains for non assignment so i use a local copy.

Isnt it smart enough to detect that its being assinged in an extern dll
call
as declared in DllImport?




"Nicholas Paldino [.NET/C# MVP]"
wrote
in message Mr. Tickle,

What is the declaration of the original function? If it takes a
pointer, then you should be using "ref" for the parameters, and
not
out.
Also, does the function take a 64-bit integer? Long in C is a 32-bit
integer, where in .NET it is a 64-bit integer.

The STDCALL mechanism should work if you are using it.

Hope this helps.


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

I am calling an unmanaged DLL like follows in a Class wrapper.

namespace blah
{
[DllImport("DLLName.DLL", EntryPoint="BlahEntryPoint",
CallingConvention=CallingConvention.ThisCall]
public static extern void blahFn(out long blah, out long blah2);

class sealed ClassBlah
{
private ClassBlah()
{
}

}
}

Why have I to call this with the CallingConvention of ThisCall, its
just
normal "C" functions in a DLL. Why wont StdCall work?

What happens with other calling conventions is that the out parameters
are
NOT set, yet with ThisCall, they are set ok, or appear to be. I find
it
odd.



Thanks
 
M

Mr.Tickle

But I still dont understand why Thiscall works and the others dont.


Nicholas Paldino said:
Mr.Tickle,

In this case, you definitely want to use the int type, and pass it by
reference. Your declaration should look like this:

[DllImport("somedll.dll")]
public static extern int someFn(ref int someIDhere, ref int someIDhere2, ref
int someIDhere3);


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

Mr.Tickle said:
typedef SOME_ID long

SOME_ID someFn(SOME_ID* someIDhere, SOME_ID* someIDhere2, SOME_ID*
someIDhere3);

something like that.


in message news:[email protected]...
If you could post the original function declaration, it would help
tremendously.


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

i mean "out" oops.

ref in the same dll wrapper still gets the output but only with thiscall
convention.


I also have a problem with "ref" paramaters, it says they need to be
assigned to before used

Yet i have them used as "ref" in dll extern calls and checked for non
NULL
after yet it complains for non assignment so i use a local copy.

Isnt it smart enough to detect that its being assinged in an exter
n
dll
call
as declared in DllImport?




"Nicholas Paldino [.NET/C# MVP]"
wrote
in message Mr. Tickle,

What is the declaration of the original function? If it
takes
a
pointer, then you should be using "ref" for the parameters, and not
out.
Also, does the function take a 64-bit integer? Long in C is a 32-bit
integer, where in .NET it is a 64-bit integer.

The STDCALL mechanism should work if you are using it.

Hope this helps.


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

I am calling an unmanaged DLL like follows in a Class wrapper.

namespace blah
{
[DllImport("DLLName.DLL", EntryPoint="BlahEntryPoint",
CallingConvention=CallingConvention.ThisCall]
public static extern void blahFn(out long blah, out long blah2);

class sealed ClassBlah
{
private ClassBlah()
{
}

}
}

Why have I to call this with the CallingConvention of
ThisCall,
its
just
normal "C" functions in a DLL. Why wont StdCall work?

What happens with other calling conventions is that the out
parameters
are
NOT set, yet with ThisCall, they are set ok, or appear to be.
I
find
it
odd.



Thanks
 
W

Willy Denoyette [MVP]

Because in you definition you only pass two arguments, while you need tree (an int, and to poiters).
It works while using the thiscall calling convention, because the interop layer passes as first argument an int for the this pointer
(three int's are reserved on the stack).

Willy.

Mr.Tickle said:
But I still dont understand why Thiscall works and the others dont.


Nicholas Paldino said:
Mr.Tickle,

In this case, you definitely want to use the int type, and pass it by
reference. Your declaration should look like this:

[DllImport("somedll.dll")]
public static extern int someFn(ref int someIDhere, ref int someIDhere2, ref
int someIDhere3);


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

Mr.Tickle said:
typedef SOME_ID long

SOME_ID someFn(SOME_ID* someIDhere, SOME_ID* someIDhere2, SOME_ID*
someIDhere3);

something like that.


in message If you could post the original function declaration, it would help
tremendously.


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

i mean "out" oops.

ref in the same dll wrapper still gets the output but only with thiscall
convention.


I also have a problem with "ref" paramaters, it says they need to be
assigned to before used

Yet i have them used as "ref" in dll extern calls and checked for non
NULL
after yet it complains for non assignment so i use a local copy.

Isnt it smart enough to detect that its being assinged in an exter n
dll
call
as declared in DllImport?




"Nicholas Paldino [.NET/C# MVP]"
wrote
in message Mr. Tickle,

What is the declaration of the original function? If it
takes
a
pointer, then you should be using "ref" for the parameters, and not
out.
Also, does the function take a 64-bit integer? Long in C is a
32-bit
integer, where in .NET it is a 64-bit integer.

The STDCALL mechanism should work if you are using it.

Hope this helps.


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

I am calling an unmanaged DLL like follows in a Class wrapper.

namespace blah
{
[DllImport("DLLName.DLL", EntryPoint="BlahEntryPoint",
CallingConvention=CallingConvention.ThisCall]
public static extern void blahFn(out long blah, out long blah2);

class sealed ClassBlah
{
private ClassBlah()
{
}

}
}

Why have I to call this with the CallingConvention of ThisCall,
its
just
normal "C" functions in a DLL. Why wont StdCall work?

What happens with other calling conventions is that the out
parameters
are
NOT set, yet with ThisCall, they are set ok, or appear to be. I
find
it
odd.



Thanks
 
M

Mr.Tickle

Well its suppost to match up, its just something i typed in a hurry but it
is the same number of arguments in the definition and call. on teh real
code.


the dll definition would be

typedef SOMEVAL long
typedef SOME_ID long

SOME_ID someFn(SOMEVAL someVal, SOME_ID* someIDhere, SOME_ID* someIDhere2,
SOME_ID* someIDhere3);


so im calling with for example out of my memory

DllImport("somedll.dll", EntryPoint="blahFn",
CallingConvention=CallingConvention.ThisCall)]
public static extern int someFn(long someVal, ref someIDhere, ref long
someIDhere2, ref long someIDhere3);
// i know this should be int instead of long but curious why the long works
with thiscall and not the others or just not work at all.

long someVal;
long ID someID,
anotherID,
yetAnohterID,
andAnotherID;

someID = blahFn(someVal, ref anotherID, ref yetAnotherID, ref andAnotherID);

Only thiscall works, others dont, so if i understand it right I change the
definition from long to int for 32bit instead of the 64bit on the .net side,
and then i can use stdcall as normal.




Willy Denoyette said:
Because in you definition you only pass two arguments, while you need tree (an int, and to poiters).
It works while using the thiscall calling convention, because the interop
layer passes as first argument an int for the this pointer
(three int's are reserved on the stack).

Willy.

But I still dont understand why Thiscall works and the others dont.


in message news:[email protected]...
Mr.Tickle,

In this case, you definitely want to use the int type, and pass it by
reference. Your declaration should look like this:

[DllImport("somedll.dll")]
public static extern int someFn(ref int someIDhere, ref int
someIDhere2,
ref
int someIDhere3);


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

typedef SOME_ID long

SOME_ID someFn(SOME_ID* someIDhere, SOME_ID* someIDhere2, SOME_ID*
someIDhere3);

something like that.


"Nicholas Paldino [.NET/C# MVP]"
wrote
in message If you could post the original function declaration, it would help
tremendously.


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

i mean "out" oops.

ref in the same dll wrapper still gets the output but only with
thiscall
convention.


I also have a problem with "ref" paramaters, it says they need
to
be
assigned to before used

Yet i have them used as "ref" in dll extern calls and checked for
non
NULL
after yet it complains for non assignment so i use a local copy.

Isnt it smart enough to detect that its being assinged in an
exter
n
dll
call
as declared in DllImport?




"Nicholas Paldino [.NET/C# MVP]"
<[email protected]>
wrote
in message Mr. Tickle,

What is the declaration of the original function? If it takes
a
pointer, then you should be using "ref" for the parameters, and
not
out.
Also, does the function take a 64-bit integer? Long in C is a
32-bit
integer, where in .NET it is a 64-bit integer.

The STDCALL mechanism should work if you are using it.

Hope this helps.


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

I am calling an unmanaged DLL like follows in a Class wrapper.

namespace blah
{
[DllImport("DLLName.DLL", EntryPoint="BlahEntryPoint",
CallingConvention=CallingConvention.ThisCall]
public static extern void blahFn(out long blah, out long
blah2);

class sealed ClassBlah
{
private ClassBlah()
{
}

}
}

Why have I to call this with the CallingConvention of ThisCall,
its
just
normal "C" functions in a DLL. Why wont StdCall work?

What happens with other calling conventions is that the out
parameters
are
NOT set, yet with ThisCall, they are set ok, or appear to
be.
I
find
it
odd.



Thanks
 

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