-
Jan 15th, 2025, 10:38 AM
#1
Thread Starter
Fanatic Member
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
-
Jan 19th, 2025, 09:09 AM
#2
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
-
Jan 19th, 2025, 07:54 PM
#3
Re: How to call embedded dll ?
 Originally Posted by greatchap
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|