首页 > 解决方案 > Converting some VB.NET code to C# - Need Advice

问题描述

I am working with the original MMO that ever opened called VZones and they have a dll file that allows you to call the functions from this dll to do certain things within the program.

I have found some VB.NET code and the problem I am having is converting it to a way that will work with C# (using VS 2017)

You setup the DLL calls with this, which works fine:

[DllImport("vzdapi.dll", CharSet = CharSet.Ansi, SetLastError = true)]
public static extern string DapiGetText(string BufferIn, int LengthIn, int Timeout);

The problem is I need to convert this VB.NET code that is from someone else to what will work in C#. I have tried a few different options but the program just closes and never gives me an error.

object obj1 = (object)new string(char.MinValue, 32678);
vzText.Text = this.DapiGetText(Conversions.ToString(obj1), Strings.Len(RuntimeHelpers.GetObjectValue(obj1)), 1500);

The problem arises in the Conversions.ToString part, which I have of course tried to just make obj1.ToString() and that may work fine but the place I think is getting caught up is when it tries to run the middle code "Strings.Len(RuntimeHelpers.GetObjectValue(obj1))".

Does anyone have any help into getting this into something that will work in C#? Thanks!

标签: c#vb.net

解决方案


我想知道是否有必要进行转换,因为您定义的初始对象已经是一个字符串,请尝试以下解决方案:

string obj = new string(char.MinValue, Int16.MaxValue);
vzText.Text = this.DapiGetText(obj, obj.Length, 1500);

推荐阅读