Newbie to RUST, looking for some advice


#1

Hi forum,
I’m new to Rust and really like the approach behind it. I have never been so experienced in C/c++ that I could be sure that my programs will not just crash due to some memory problems and it seems that Rust solves a lot of my problems.
I’m just trying to implement my first little project under Windows and came to a general problem. I need to call
https://msdn.microsoft.com/en-us/library/windows/desktop/aa379159(v=vs.85).aspx in order to obtain the SID of a user. I just used cargo and became familiar with the concept of crates so that I should in principle be able to call into a Windows API. However, I’m lost with the translation of all the data types that the function expects.

BOOL WINAPI LookupAccountName(
In_opt LPCTSTR lpSystemName,
In LPCTSTR lpAccountName,
Out_opt PSID Sid,
Inout LPDWORD cbSid,
Out_opt LPTSTR ReferencedDomainName,
Inout LPDWORD cchReferencedDomainName,
Out PSID_NAME_USE peUse
);
I searched this forum and found Tidy pattern to work with LPSTR (mutable char array) but that does only seem to solve a small part of my problem. Is there anybody here able (and willing) to help me out with a working example as a starting point? Any help would be greatly appreciated.
Thanks
Norbert


#2

Have a look at the WinAPI crate for examples.

While that specific function is not implemented there yet, the meaning is that it should contain all Windows API functions. So I’d recommend to directly implement it in that crate and then file a PR on github.


#3

Hi troplin,
Thanks for the answer. I looked at the WinAPI crate before. As far as I understood it, it aims to provide a kind of wrapper around all WinAPI functions. However, as you mentioned, the function that I’m after is not yet implemented. And honestly, I think that implementing it there goes a little beyond my competence.
Norbert


#4

I’d rather say it’s easier, because that crate already provides all necessary types.
Have a look at: https://github.com/retep998/winapi-rs/blob/master/lib/advapi32/src/lib.rs#L502
You just have to insert the correct definition there.

If you need help, just ask.


#5

I see your point. Maybe it’s really more easy to realize it there.
But here directly comes my first question:
Why ist the function listed in https://github.com/retep998/winapi-rs/blob/master/lib/advapi32/src/lib.rs#L502 rather than under https://github.com/retep998/winapi-rs/blob/master/lib/authz/src/lib.rs?


#6

Go to that link, scroll down to the “Requirements” table and you will see why :wink:


#7

Hm, I spent some time to look thru the structure of the repository and it really seems to be that simple :slight_smile:
I added the functions that I need to https://github.com/norbert78/winapi-rs/blob/feature/fn-LookupAccountName/lib/advapi32/src/lib.rs#L501-L506
Is that all I need to do in order to use the functions via this crate? I would appreciate if you could have a short look before I open a Pull Request…
Thanks!


#8

I suggest that you just try it out, that’s why you’re doing all this, no? :wink:
You should do that anyway before you open a PR.

You should probably adjust the formatting a bit to be consistent with the rest, i.e. indent the parameters and split the parameter list to multiple lines.
Otherwise it look good to me.
Not sure if you should also add it to ./tests/advapi32.rs

But in case of doubt just go ahead, you can still make corrections afterward