使用元帅或其他方式在VB中使用指针 [英] Pointers in VB using the Marshal or any otherway

查看:135
本文介绍了使用元帅或其他方式在VB中使用指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以帮助我将这段代码转换为Visual Basic,尤其是那些指针声明吗?请帮忙

Can some body help me to convert this code into visual basic especially those pointers declaration. please help

private unsafe void PutImage(ref System.IntPtr pPtr, int imageBufferSize, ADVANTAGELib.AdvImageType imageType)
{
    byte** ppResBuffer = (byte**)pPtr;
    byte* pResBuffer = *ppResBuffer;
     
    int lPictureWidth = this.signatureImagePictureBox.Width;
    int lPictureHeight = this.signatureImagePictureBox.Height;
     
    using (MemoryStream imageStream = new MemoryStream(imageBufferSize))
    {
        using (BinaryWriter w = new BinaryWriter(imageStream))
        {
            for (int i = 0; i < imageBufferSize; i++)
            {
                w.Write((byte)pResBuffer[i]);
            }
             
            Bitmap lPicture = new Bitmap(Image.FromStream(imageStream), lPictureWidth, lPictureHeight);
             
            if (imageType == ADVANTAGELib.AdvImageType.AdvSignatureImage)
            {
                this.signatureImagePictureBox.Image = lPicture;
            }
            else
            {
                this.cardholderImagePictureBox.Image = lPicture;
            }
             
            System.Runtime.InteropServices.Marshal.FreeCoTaskMem((System.IntPtr)pResBuffer);
        }
    }
     
    System.GC.Collect();
}

推荐答案

我不知道它在VB中是如何工作的,但是这就是我在C#中的修改方式,应该是代码可以轻松转换为VB!
I don''t know how it works in VB but this is how I''d alter this in C#, which should be code that can be easily converted to VB!
private void PutImage(IntPtr pPtr, int imageBufferSize, AdvImageType imageType)
{
    byte[] data = new byte[imageBufferSize];
    Marshal.Copy(pPtr, data, 0, imageBufferSize);
    using (MemoryStream imageStream = new MemoryStream(imageBufferSize))
    {
        using (BinaryWriter w = new BinaryWriter(imageStream))
        {
            for (int i = 0; i < imageBufferSize; i++)
                w.Write(data[i]);
            Bitmap lPicture = new Bitmap(
                Image.FromStream(imageStream), 
                signatureImagePictureBox.Width, 
                signatureImagePictureBox.Height);
            if (imageType == AdvImageType.AdvSignatureImage)
                signatureImagePictureBox.Image = lPicture;
            else
                cardholderImagePictureBox.Image = lPicture;
            Marshal.FreeCoTaskMem(pPtr);
        }
    }
}


我已经通过删除ref更改了第一个参数.在这种情况下,将指针传递给指针似乎毫无意义.我还使用了Marshal类从数据创建托管数组,因为那样就无需使用不安全的代码!

当然未经测试,但可以编译,应该可以.

删除了不再需要的不安全关键字:doh!


I have changed the first parameter by removing the ref. There seems little point in passing a pointer to a pointer in this scenario. I have also used the Marshal class to create a managed array from the data instead as then there is no need to use unsafe code!

Untested of course, but it compiles and should be OK.

Removed the unsafe keyword as no longer needed :doh!


尽管我没有答案,但我可以为您提供一些信息和选项.
首先,为什么要将其转换为VB?由于VB的不安全代码不如C#好,因此将这段代码保存在C#程序集中并在VB项目中引用它是更有意义的,因此可以从VB代码中调用此函数.

如果您真的想将此代码转换为VB,建议阅读以下文章: IntPtr结构 [ ^ ].

最后一句话,我无法真正弄清楚您的代码在做什么,但是您可能要考虑完全重写该代码,以使其不再使用指针(或调用GC.Collect [
Though I do not have the answer I can provide you with some information and options.
First of all, why convert this to VB? Since VB is not as good with unsafe code as C# it would make more sense to keep this code in a C# assembly and reference that in a VB project so you can call this function from VB code.

If you would really want to convert this code to VB I suggest reading the following article: How to do pointers in Visual Basic[^].

Other than that the closest you can get to pointers in VB is probably the IntPtr Structure[^].

A last remark, I can''t really figure out what your piece of code is doing, but you might want to consider rewriting that code completely so it doesn''t use pointers anymore (or call GC.Collect[^]). Generally you''d never need them in VB.


谢谢.我将尝试将其限制为DLL,但我不擅长引用DLL文件.如果我只能转换这些声明并将其引用回去,我认为它可能会起作用,但我正在努力
Thanks I will try to convent it to a DLL, but i am not good to referencing to DLL files. If i can convert only those declarations and refer to them back, i think it might work but i am struggling


这篇关于使用元帅或其他方式在VB中使用指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆