Forums

P/Invoke

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Re: P/Invoke

Postby Charles » Sun Jan 02, 2011 11:39 pm

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

Postby hopscc » Mon Jan 03, 2011 1:16 am

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
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: P/Invoke

Postby torial » Mon Jan 03, 2011 9:57 am

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

Postby torial » Mon Jan 03, 2011 10:33 am

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.

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

Postby Charles » Mon Jan 03, 2011 11:25 am

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

Postby Charles » Mon Jan 03, 2011 12:38 pm

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

Postby hopscc » Tue Jan 04, 2011 5:04 am

Heres the cobra variant compile and run
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

Postby hopscc » Tue Jan 04, 2011 5:25 am

Chuck,
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

Postby Charles » Tue Jan 04, 2011 11:53 am

Up until now, it's never been used for methods--only types:
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

Postby hopscc » Wed Jan 05, 2011 3:16 am

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..
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

PreviousNext

Return to Discussion

Who is online

Users browsing this forum: No registered users and 8 guests

cron