简体   繁体   English

此C ++代码的C#等价物是什么(带有InPtr)

[英]What is the C# equivalent of this C++ code (with InPtr)

I'm implementing a Custom Credential Provider in C# . 我正在C#中实现自定义凭据提供程序 I'm using a C++ project as example. 我以一个C ++项目为例。 This piece of C++ code provides an image to Windows. 这段C ++代码为Windows提供了一个映像。 The way I see it phbmp is a pointer to the image-bitmap. 我看到phbmp的方式是指向图像位图的指针。 The code either updates the pointer so it points to a new bitmap (read from Resource) or it loads the bitmap to the address pointed by phbmp . 代码要么更新指针,使其指向新的位图(从Resource读取),要么将位图加载到phbmp指向的地址。 I'm not sure if the pointer itself is changed or not . 我不确定指针本身是否被更改

// Get the image to show in the user tile
HRESULT CSampleCredential::GetBitmapValue(DWORD dwFieldID, _Outptr_result_nullonfailure_ HBITMAP *phbmp)
{
    HRESULT hr;
    *phbmp = nullptr;

    if ((SFI_TILEIMAGE == dwFieldID))
    {
        HBITMAP hbmp = LoadBitmap(HINST_THISDLL, MAKEINTRESOURCE(IDB_TILE_IMAGE));
        if (hbmp != nullptr)
        {
            hr = S_OK;
            *phbmp = hbmp;
        }
        else
        {
            hr = HRESULT_FROM_WIN32(GetLastError());
        }
    }
    else
    {
        hr = E_INVALIDARG;
    }

    return hr;
}

Below is the C# equivalent I'm implementing: 以下是我正在实现的C#等效项:

public int GetBitmapValue(uint dwFieldID, IntPtr phbmp)
{
    if (dwFieldID == 2)
    {
        Bitmap image = Resource1.TileImage;

        ImageConverter imageConverter = new ImageConverter();
        byte[] bytes = (byte[])imageConverter.ConvertTo(image, typeof(byte[]));

        Marshal.Copy(bytes, 0, phbmp, bytes.Length);

        return HResultValues.S_OK;
    }

    return HResultValues.E_INVALIDARG;
}

What I'm trying to do: 我正在尝试做的是:

  1. Load the image from resource (this works, it has the correct length) 从资源加载图像(此方法有效,长度正确)
  2. Convert the Bitmap to an array of bytes 将位图转换为字节数组
  3. Copy these bytes to the address pointed by phbmp 将这些字节复制到phbmp指向的地址

This crashes, I assume because of memory-allocation. 由于内存分配,我认为这会崩溃。

The parameters in this method are defined by an interface (in CredentialProvider.Interop.dll , which is provided by Microsoft - I think). 此方法中的参数由接口定义(在Microsoft提供的CredentialProvider.Interop.dll中 -我认为)。 So I'm pretty sure it's correct and phbmp is not an out -parameter. 因此,我非常确定这是正确的,并且phbmp不是out- parameter

Because it is not an out-parameter I can not change phbmp to let it point to my bitmap, right? 因为它不是输出参数,所以我无法更改phbmp使其指向我的位图,对吗? I have assigned phbmp to Bitmap.GetHbitmap() and that doesn't crash but it isn't working either. 我已将phbmp分配给Bitmap.GetHbitmap() ,并且不会崩溃,但也无法正常工作。 I assume that the change to phbmp is only local in this method. 我假设对phbmp的更改仅在此方法中是本地的。

I can understand that it is not possible to alloc memory to a predefined address. 我可以理解不可能将内存分配给预定义的地址。 It's the other way around: you alloc memory and get an pointer to it. 这是另一回事:分配内存并获得指向它的指针。 But then this change is local again. 但是,此更改再次是本地的。 How does this work? 这是如何运作的?

Although some people agreed that IntPtr should be an out-parameter (see comments in https://syfuhs.net/2017/10/15/creating-custom-windows-credential-providers-in-net/ ) the answer was actually: 尽管有些人认为IntPtr应该是一个外参数(请参见https://syfuhs.net/2017/10/15/creating-custom-windows-credential-providers-in-net/中的注释),但答案实际上是:

var bmp = new Bitmap(imageStream);  
Marshal.WriteIntPtr(phbmp, bmp.GetHbitmap()); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM