Forums
P/Invoke
24 posts
• Page 2 of 3 • 1, 2, 3
Re: P/Invoke
Hops, do you have an example that wouldn't make the Cobra test suite beep every time we run it?
- Charles
- Posts: 2515
- Location: Los Angeles, CA
Re: P/Invoke
Sorry no
- I just took that one cos fm the mag article it looked like the simplest call to use for a q-and-d test.
and so it turned out
if anyone can point me to a another (silent) simple call I can gen a full ticket+patch
- I just took that one cos fm the mag article it looked like the simplest call to use for a q-and-d test.
and so it turned out
if anyone can point me to a another (silent) simple call I can gen a full ticket+patch
- hopscc
- Posts: 632
- Location: New Plymouth, Taranaki, New Zealand
Re: P/Invoke
Hopscc, thanks for what you did in this. I will try to have a safe test call provided sometime today.
- torial
- Posts: 229
- Location: IA
Re: P/Invoke
I'm flying a little blind (haven't applied the patch to my local) so my sample may fail in Cobra. I've included the C# variant which I have confirmed works.
Here's the C# (minus the assert):
use System.Runtime.InteropServices
class PInvokeTst
#PInvoke entry point
def getUserName(lpBuffer as byte[] has MarshalAs(UnmanagedType.LPArray), nSize as Int32[] has MarshalAs(UnManagedType.LPArray)) as bool is shared, extern
has DllImport("Advapi32.dll", setLastError = true, entryPoint = "GetUserName", exactSpelling=false)
pass
def main is shared
userNameBytes = byte[](256)
len = int[](1)
len[0] = 256
.getUserName(userNameBytes,len)
userName = System.Text.Encoding.ASCII.getString(userNameBytes).trim(c'${1}')
#For confirmation: alternative .Net approach:
userCred = System.Security.Principal.WindowsIdentity.getCurrent().name.toString
if userCred.contains("\") # this should be two backslashes, but displays as one in forum code.
userCred = userCred.split("\")[1] # this should be two backslashes, but displays as one in forum code.
assert userCred == userName, "WinAPI call doesn't match the .Net Principal"
Here's the C# (minus the assert):
- Code: Select all
using System.Runtime.InteropServices;
class PInvokeTst
[DllImport("Advapi32.dll", EntryPoint = "GetUserName",
ExactSpelling = false, SetLastError = true)]
static extern bool GetUserName(
[MarshalAs(UnmanagedType.LPArray)] byte[] lpBuffer,
[MarshalAs(UnmanagedType.LPArray)] Int32[] nSize);
public PInvokeTst()
{
byte[] userNameBytes = new byte[256];
int[] len = new int[1];
len[0] = 256;
GetUserName(userNameBytes, len);
string userName = System.Text.Encoding.ASCII.GetString(userNameBytes).Trim('\0');
string userCred = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
if (userCred.Contains("\\"))
{
userCred = userCred.Split('\\')[1];
}
Console.WriteLine(userName +" " + userCred);
}
- torial
- Posts: 229
- Location: IA
Re: P/Invoke
Hops, the web site http://pinvoke.net has a bunch of these. Poking around, I found http://pinvoke.net/default.aspx/user32.IsCharLower.
- Charles
- Posts: 2515
- Location: Los Angeles, CA
Re: P/Invoke
Btw C# does not require a body on methods that are marked with DllImport and we already have methods that don't require a body ("def foo is abstract"). I think the correct approach is to do the same thing with DllImport methods in Cobra.
- Charles
- Posts: 2515
- Location: Los Angeles, CA
Re: P/Invoke
Heres the cobra variant compile and run
Outputs
use System.Runtime.InteropServices
class PInvokeTst
#PInvoke entry point
def getUserName(lpBuffer as uint8[] has MarshalAs(UnmanagedType.LPArray), nSize as int32[] has MarshalAs(UnmanagedType.LPArray)) as bool is shared, extern
has DllImport("Advapi32.dll", setLastError = true, entryPoint = "GetUserName", exactSpelling=false)
pass
def main is shared
userNameBytes = uint8[](256)
len = int[](1)
len[0] = 256
.getUserName(userNameBytes,len)
userName = System.Text.Encoding.ascii.getString(userNameBytes).trim(c'${1}') # c ' \ 0'
print userName
#For confirmation: alternative .Net approach:
userCred = System.Security.Principal.WindowsIdentity.getCurrent.name.toString
print userCred
if userCred.contains('') # dbl backslash in quotes
userCred = userCred.split(c'')[1] # dbl backslash in quotes
assert userCred == userName, "WinAPI call doesn't match the .Net Principal call"
Outputs
- Code: Select all
$ cobc0 pinvokeT.cobra
hops
KERERU\hops
- hopscc
- Posts: 632
- Location: New Plymouth, Taranaki, New Zealand
Re: P/Invoke
Chuck,
Is there any situation where a method declared extern would be allowed to have statements ?
Is there any situation where a method declared extern would be allowed to have statements ?
- hopscc
- Posts: 632
- Location: New Plymouth, Taranaki, New Zealand
Re: P/Invoke
Up until now, it's never been used for methods--only types:
For P/Invoke, I think it should be sufficient to to have "is shared has DllImport(...)".
- Code: Select all
./Snapshot/Cobra.Lang/Misc.cobra:48: class CobraDirectString is extern
./Snapshot/Cobra.Lang/NativeExtern.cobra:10: class CobraImp is extern
./Snapshot/Cobra.Lang/NativeExtern.cobra:36: interface ICallable is extern
For P/Invoke, I think it should be sufficient to to have "is shared has DllImport(...)".
- Charles
- Posts: 2515
- Location: Los Angeles, CA
Re: P/Invoke
Yes, I realise its not been applied to methods up til now but given that
- it can be applied to methods without a diagnostic and that
- such application does make the (local) declaration of such a method a descriptor/placeholder/forward-reference for a method defined elsewhere
I'm still wondering if anyone thinks that there is any situation where such a method (i.e one declared extern) would
make any sense to have statements.
The DllImport attribute is nicely specific to the .Net platform but it doesnt mean much anywhere else and doesnt exactly self describe
that the method being annotated needs to be bereft of statements..
- it can be applied to methods without a diagnostic and that
- such application does make the (local) declaration of such a method a descriptor/placeholder/forward-reference for a method defined elsewhere
I'm still wondering if anyone thinks that there is any situation where such a method (i.e one declared extern) would
make any sense to have statements.
The DllImport attribute is nicely specific to the .Net platform but it doesnt mean much anywhere else and doesnt exactly self describe
that the method being annotated needs to be bereft of statements..
- hopscc
- Posts: 632
- Location: New Plymouth, Taranaki, New Zealand
24 posts
• Page 2 of 3 • 1, 2, 3
Who is online
Users browsing this forum: No registered users and 6 guests