Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GetLastError() fails. #189

Closed
krjw-eyev opened this issue Mar 28, 2021 · 5 comments
Closed

GetLastError() fails. #189

krjw-eyev opened this issue Mar 28, 2021 · 5 comments
Labels
blocked Waiting on an external issue to be resolved bug Something isn't working as expected

Comments

@krjw-eyev
Copy link
Contributor

In this simple example GetLastError() returns 0 although it should return 100. I might do something wrong here.

import 'dart:ffi';

final _kernel32 = DynamicLibrary.open('kernel32.dll');

int GetLastError() {
  final _GetLastError = _kernel32.lookupFunction<Uint32 Function(), int Function()>('GetLastError');
  return _GetLastError();
}

void SetLastError(int dwErrCode) {
  final _SetLastError = _kernel32.lookupFunction<Void Function(Uint32 dwErrCode), void Function(int dwErrCode)>('SetLastError');
  return _SetLastError(dwErrCode);
}

main(List<String> args) async {
  SetLastError(100);
  await Future.delayed(Duration(milliseconds: 200));
  final ret = GetLastError();
  print(ret);
}

This could be related to how ffi works in terms of calling native functions.

@timsneath timsneath added bug Something isn't working as expected quality labels Mar 28, 2021
@timsneath
Copy link
Contributor

I've noticed this too, although I wasn't smart enough to try setting an error to confirm this. @dcharkes this might potentially be an FFI issue if other calls overwrite the thread last error value? https://docs.microsoft.com/en-us/windows/win32/api/errhandlingapi/nf-errhandlingapi-getlasterror

@krjw-eyev
Copy link
Contributor Author

That was my initial thought too! node-ffi has the same problem too node-ffi/node-ffi#261.
Unfortunately this has not been touched in nearly 4 years.

Do you think we should report this to the dart-lang team? I have tried to figure out where this might happen...
https://github.com/dart-lang/sdk/blob/master/runtime/lib/ffi.cc

Here they call SetLastError
https://github.com/dart-lang/sdk/blob/master/runtime/lib/ffi_dynamic_library.cc

@dcharkes
Copy link

Hey! This is a known issue.

this might potentially be an FFI issue if other calls overwrite the thread last error value

Yes it is. Any thread local storage can be clobbered by other calls happening in the mean time. (E.g. the VM might do a garbage collection or something else and call OS API's in the mean time.)

We're tracking this on dart-lang/sdk#38832.

@krjw-eyev
Copy link
Contributor Author

Should we close this issue then, since it has nothing to do with this lib? Maybe add something to README so people know about this?

@timsneath timsneath added the blocked Waiting on an external issue to be resolved label Mar 29, 2021
@Sunbreak
Copy link
Contributor

We'd better hide the GetLastError until it works: https://github.com/timsneath/win32/pull/382/files

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
blocked Waiting on an external issue to be resolved bug Something isn't working as expected
Projects
None yet
Development

No branches or pull requests

4 participants