Results 1 to 3 of 3

Thread: How to call embedded dll ?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2004
    Location
    India
    Posts
    526

    Question How to call embedded dll ?

    Hello Everyone,

    I have a .net program that does a few tasks and it does not use any third party dll. I am using .net framework 4.0.

    I just came across one task which requires me to read a json file/output. Since .net 4.0 does not support json natively I may need to use Newtonsoft.Json.dll. However, I do not want to distribute this dll separately and I have added this in my project as an embedded resource.

    How do I call this resource and use its functions like we would do with any other dll that we add as reference in the project? I want to instantiate the classes in dll and then perform json related tasks.

    Thank you,

    Cheers,
    GR

  2. #2
    Hyperactive Member
    Join Date
    Jul 2022
    Location
    Buford, Ga USA
    Posts
    491

    Re: How to call embedded dll ?

    You aren't forced to use NewtonSoft as there was a native JSON parser then System.Runtime.Serialization.

    Code:
    Imports System.IO
    Imports System.Runtime.Serialization
    Public Class Form1
    
        ' what to read the data into
        <DataContract()> _
        Public Class Person
            <DataMember()> Public Property name As String
            <DataMember()> Public Property language As String
            <DataMember()> Public Property id As String
            <DataMember()> Public Property bio As String
            <DataMember()> Public Property version As Double
        End Class
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            ' open the test JSON file and serialize it
            Dim jsonData As String
            With New StreamReader("E:\Downloads\128k_Test.json")
                jsonData = .ReadToEnd
                .Close()
            End With
    
            Dim persons As List(Of Person)
            Dim serializer As New Json.DataContractJsonSerializer(GetType(List(Of Person)))
    
            Using ms As New MemoryStream(System.Text.Encoding.UTF8.GetBytes(jsonData))
                persons = CType(serializer.ReadObject(ms), List(Of Person))
            End Using
    
            ListBox1.Items.Clear()
            For Each person As Person In persons
                ListBox1.Items.Add("Name: " & person.name)
                ListBox1.Items.Add("    language: " & person.language)
                ListBox1.Items.Add("    id: " & person.id)
                ListBox1.Items.Add("    version: " & person.version)
            Next
        End Sub
    End Class
    screenshot: https://imgur.com/a/RwlDFCw

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,984

    Re: How to call embedded dll ?

    Quote Originally Posted by greatchap View Post
    How do I call this resource and use its functions like we would do with any other dll that we add as reference in the project?
    You don't. You need a DLL file. You would have to extract the data from the resource and save it as a file, but many antivirus tools will flag that as potentially malicious behaviour, because hidden executables is something that malicious software uses. Assuming you're able to extract the data and save the file, you would then have to load the DLL and access the types it contains using Reflection, which is more laborious and less safe than referencing the DLL at compile time. If you actually need it, just reference the DLL and deploy it with your app like everyone else does. It's how software works.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width