question

AlanEmpeigne-1555 avatar image
0 Votes"
AlanEmpeigne-1555 asked RLWA32-6355 commented

Reading/Calling String variable from .dll file

Hi there,

I have recently created a New.dll file using VS 2022 in C# for later implementation into my VB6 program - I have recently taken over software development!

For the meantime, I have created a test software in VS22 C# to test if the New.dll file works.
So far so good, I have added Reference from my test software to the New.dll and added "using [namespace]" to my test program.

Issue: I need to read a string variable from the New.dll file and display it on a textbox on my test software.
How can I read a string variable which is used in New.dll and display on my test software?

FYI - Here is the function where the string variable is processed on my New.dll file - m_sDataSent is the string variable.

public void SendData(string str)
{
if (connection == true)
{
if (Comms != null)
Comms(this, m_sDataSent);
}
}

Many Thanks!

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JackJJun-MSFT avatar image
0 Votes"
JackJJun-MSFT answered AlanEmpeigne-1555 commented

@AlanEmpeigne-1555, as Bruce said, we could set public property and public method to get the value from the dll.

I make a code example for you and hope it could help you.

New.dll:(.NET Framework Class Library)

 namespace New
 {
     public class Example
     {
         public string Getvalue { get; set; }
         public void SendData()
         {
    
             if(condition)
             {
                 string m_sDataSent = "Hello";
                 Getvalue = m_sDataSent;
             }
         }
     }
 }

Winform project:


 private void button1_Click(object sender, EventArgs e)
         {
             Example example = new Example();
             example.SendData();
             textBox1.Text = example.Getvalue;
         }

After clicking the button, the textbox will show "Hello" text.

195370-image.png

Best Regards,
Jack



If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



image.png (2.3 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks JackJJun-MSFT for the example!

0 Votes 0 ·
Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered AlanEmpeigne-1555 commented

In c#, external modules can only access public strings. You would need to make the string public, or supply a public method to set it.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for reply @Bruce-SqlWork - I just reviewed my .dll code and noticed the error "private string m_sDataSent = "";"

I will try it next.

0 Votes 0 ·
RLWA32-6355 avatar image
0 Votes"
RLWA32-6355 answered RLWA32-6355 commented

I have recently created a New.dll file using VS 2022 in C# for later implementation into my VB6 program...


You should be aware that a VB6 application cannot directly use a .net class library (DLL) unless the class library exposes its classes and methods through COM. There are many available references about how to do this. One starting point is COM Wrappers. Additionally, since VB6 applications are 32-bit you need to take care that the COM registration for your .net class library is made in the 32-bit view of the registry.


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Yes, RLWA32-6355 absolutely right. Luckily, I read a thread before compiling the .dll file so it is set as COM-Visible.

I am sure VB6 will throw me many more challenges as I quickly understood of all it limitations compared to Visual Studio IDE...

0 Votes 0 ·
RLWA32-6355 avatar image RLWA32-6355 AlanEmpeigne-1555 ·

In addition to checking the box for "Make assembly COM-Visible" you also need to check the box "Register for COM interop".

Visual Studio performs per-machine COM registration in the registry's HKEY_LOCAL_MACHINE key. This requires that you run Visual Studio as an Administrator so that it has the necessary privileges to perform the registration. Otherwise, the class library will be created but COM registration will fail. Since VS 2022 is a 64-bit application it will perform COM registration for a class library built as "AnyCPU" in the 64-bit view of the registry. Consequently, a 32-bit application will be unable to instantiate COM objects due to missing registration information. To avoid this issue with VS2022 ensure that you build your class library as "x86" for use with 32-bit COM clients like VB6.

0 Votes 0 ·