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

Hard time getting CopyFileEx to work #57

Closed
wldevries opened this issue Jan 23, 2021 · 3 comments
Closed

Hard time getting CopyFileEx to work #57

wldevries opened this issue Jan 23, 2021 · 3 comments
Assignees

Comments

@wldevries
Copy link

wldevries commented Jan 23, 2021

First of all I'm terrible at C++ and interop, so excuse me for any dumb remarks in this issue. I've used pinvoke.net before to use CopyFileEx in our code base. I tried replacing the dllimport with CsWin32. I ran into several issues.

The callback argument lpProgressRoutine is of type LPPROGRESS_ROUTINE. When opening the reference of CopyFileEx in Visual Studio after generating it by adding it to NativeMethods.txt the type LPPROGRESS_ROUTINE remains white and cannot be opened using Go To Definition. In addition to this Visual Studio was unable to generate a method for the callback routine using quick actions. The only option was to create a field/property/variable as type object. Finally I found out that Go To Definition does work when you write out the type in a C# file in source file.

The generated type LARGE_INTEGER cannot be casted to int making it impossible to make heads or tails from the progress values in the LPPROGRESS_ROUTINE calls.

The types LPVOID and LPBOOL are generated as void* and bool* correspondingly. Since these values aren't needed passing in null for both should work (should it?), but I was unable to use the argument types I was used to: IntPtr.Zero and a bool by ref.

The last problem is that enum values in the return type of LPPROGRESS_ROUTINE seem to be missing, for which I created the issue microsoft/win32metadata#170 in the win32metadata repository.

After messing around a bit it seems the CsWin32 source generator has stopped working entirely and even after a VS restart and NuGet uninstall/install it did not seem to want to generate any more code. I'm not sure what's going on and where to look for log files.

@AArnott
Copy link
Member

AArnott commented Jan 24, 2021

Hi @wldevries. Thank you so much for giving this a try and for providing great feedback. I'm sorry your first experience wasn't better.

You've made several good points here. I will look more into them next week to see where we can improve. My goal is to provide you with a sample of how to make it work, and any fixes we may need to make here in order to make that happen.

@AArnott AArnott self-assigned this Jan 24, 2021
@AArnott
Copy link
Member

AArnott commented Feb 18, 2021

Some of the issues you raised with the generator itself have been fixed. You can try them out in the daily build (see readme). Also, please try using VS from the Preview channel, as many fixes have been made to the IDE integration of Source Generators in VS 2019 Update 9.

This seems to work:

    public unsafe void CopyFileEx(string src, string dest)
    {
        PInvoke.CopyFileEx(src, dest, CopyCallback, lpData: null, pbCancel: null, dwCopyFlags: 0);

        unsafe uint CopyCallback(long totalFileSize, long totalBytesTransferred, long streamSize, long streamBytesTransferred, uint dwStreamNumber, uint dwCallbackReason, SafeHandle hSourceFile, SafeHandle hDestinationFile, void* lpData)
        {
            throw new NotImplementedException();
        }
    }

@AArnott
Copy link
Member

AArnott commented Feb 18, 2021

I'm closing the issue to help with tracking, but comment here and we can reopen if necessary.

@AArnott AArnott closed this as completed Feb 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants