我正在尝试通过ML32中的直接逐位传输将vb 2015 Integer更改为vb 2015 UInteger。从vb2015开始,我在dll中调用vc ++ 2015函数,该函数使用内联汇编程序来实现更改。
通过这样做,我可以采用负整数,如-633593090 =& HDA3C22FE = 11011010001111000010001011111110b
并将其更改为UInteger = 11011010001111000010001011111110b =& 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
我使用这个vb 2015代码实际调用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中间变量?
BTW,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”语句列表。
答案 0 :(得分:0)
我意识到你说你只是把它作为一个练习,但有一种相对简单的方法可以在VB(或C#)中获得你想要的结果。
Function ToUInt(value As Integer) As UInteger
Return CUInt(If(value < 0, UInteger.MaxValue + value + 1, value))
End Function