vb Integer到c++到ML32到c++到vb UInteger

vb Integer to c++ to ML32 to c++ to vb UInteger

本文关键字:vb c++ UInteger ML32 Integer      更新时间:2023-10-16

我正在尝试通过ML32中的直接逐位传输将vb 2015 Integer更改为vb 2015 UInteger。在vb2015中,我在dll中调用了一个vc++2015函数,该函数使用内联汇编程序来实现更改。

通过这样做,我可以得到一个负整数,例如633593090=&HDA3C22FE=11011010001111000010001011111110b

并将其更改为UInteger=111011010001111000010001011111110b=&HDA3C22FE=3661374206

与看似简单的"uTest1=CUInt(iTest1)"相反,它为iTest1负数抛出System.OverflowException。

我有可以正常工作的代码,但我对微小改进的可能性感兴趣。

在vb 2015中,我声明对vc++2015 dll的访问权限如下:

' vb Integer --> c++ signed long --> c++ unsigned long --> vb UInteger -- Works!
'   by direct copy of bits through x86 ML32
'   4 bytes = 32 bits
<DllImport("StringTest.dll", EntryPoint:="bitConvert", SetLastError:=True, CharSet:=CharSet.Ansi, ExactSpelling:=True, CallingConvention:=CallingConvention.Cdecl)>
Private Shared Function bitConvertTester(ByRef varSInt As Integer, ByRef varUInt As UInteger) As Integer
    ' Must be "Shared"
    ' Must be "ByRef"
    ' Do not try varSInt As "Signed Integer" or varUInt As "Unsigned Integer" --> Syntax Error
    ' Leave the body of the function empty
End Function

我使用这个vb2015代码实际调用dll中的vc++2015函数:

    ' Third Method - Partial - Works!
    Dim pCTest As Color
    Dim iTest As Integer
    Dim uTest As UInteger
    Dim returnCode As Integer
    pCTest = Color.FromArgb(&HDA, &H3C, &H22, &HFE)
    iTest = pCTest.ToArgb
    System.Diagnostics.Debug.Write(iTest.ToString + vbCrLf)
    System.Diagnostics.Debug.Write(Hex(iTest) + vbCrLf)
    ' vb Integer --> c++ signed long --> c++ unsigned long --> vb UInteger
    '   by direct copy of bits through x86 ML32
    '   4 bytes = 32 bits
    ' Note: Function prototype is "ByRef", but this MUST 
    '   NOT go in this test call (causes a syntax error).
    returnCode = bitConvertTester(iTest, uTest)
    System.Diagnostics.Debug.Write(uTest.ToString + vbCrLf)
    System.Diagnostics.Debug.Write(Hex(uTest) + vbCrLf)

这里是vc++2015 dll函数本身:

    #define EXPORT_VB extern "C" __declspec(dllexport)
    // vb Integer --> c++ signed long --> c++ unsigned long --> vb UInteger -- Works!
    //   by direct copy of bits through x86 ML32
    //   4 bytes = 32 bits
    EXPORT_VB long __cdecl bitConvert(signed long *varSInt, unsigned long *varUInt)
    {
        // Intermediate variables are required for ML32 access.
        signed long varSInt1;
        unsigned long varUInt1;
        varSInt1 = *varSInt;
        __asm
        {
            mov EAX, varSInt1
            mov varUInt1, EAX
        }
        *varUInt = varUInt1;
        return 1;                   // Success Code
    }

我用变量ByRef从vb 2015调用了vc++2015 dll函数,这样dll函数就可以使用指针通过*varSInt和*varUInt根据需要更改vb 2015变量。

我的问题是:在vc++__asm块中,ML32代码是否有任何方法可以直接访问*varSInt和*varUInt,而不需要经过varSInt1和varUInt1中间变量?

顺便说一句,vb 2015代码使用:

 Imports System
 Imports System.IO
 Imports System.Text
 Imports System.Runtime.InteropServices

并且vc++2015代码使用:

 #include "stdafx.h"
 #include <stdexcept>
 #include <string.h> 

(我对Microsoft文档感到沮丧的一件事是,它没有包含使用文档所描述的功能所需的"Imports"或"#include"语句的列表)。

我意识到你说过你只是在练习,但有一种相对简单的方法可以在VB(或C#)中获得你想要的结果。

Function ToUInt(value As Integer) As UInteger
    Return CUInt(If(value < 0, UInteger.MaxValue + value + 1, value))
End Function