Setting DirectShow VSFilter file name through code - vb.net

I've been searching for a way to do this using VB.NET and DirectShowNET for many months. Thanks to the question Is there any way to control DirectShow VSFilter programmatically? asked last week, I'm on the right track but need help getting over the final hurdle.
As per the answer to the above question, I can get hold of the filter's interface and call put_FileName(). However, when I check the filter in the running graph, the file name is either junk or blank. Here's how I load the filter:
Dim clsidVobsub As New Guid("{93A22E7A-5091-45EF-BA61-6DA26156A5D0}")
Dim filterVobsub As IBaseFilter
Dim type__2 As Type = Type.GetTypeFromCLSID(clsidVobsub)
filterVobsub = DirectCast(Activator.CreateInstance(type__2), IBaseFilter)
Graph.AddFilter(filterVobsub, "My VobSub")
Dim vobSettings As ISettingsInterface = DirectCast(filterVobsub, ISettingsInterface)
Dim fn As String = "C:\Leta\testing.srt"
Dim hr as Integer = vobSettings.put_FileName(fn)
That seems to work as it should, so I think the problem lies with the way I'm trying to marshall the function call in the interface definition.
<PreserveSig()> _
Function put_FileName(<MarshalAs(UnmanagedType.LPWStr)> fn As String) As Integer
Can anybody see what I'm doing wrong?
EDIT: The ISettingsInterface is my own. It's declared like this:
<ComImport(), System.Security.SuppressUnmanagedCodeSecurity(), Guid("EBE1FB08-3957-47ca-AF13-5827E5442E56"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)> _
Public Interface ISettingsInterface
<PreserveSig()> _
Function get_FileName(<MarshalAs(UnmanagedType.LPWStr)> fn As String) As Integer
<PreserveSig()> _
Function put_FileName(<MarshalAs(UnmanagedType.LPWStr)> fn As String) As Integer
End Interface
SOLUTION: It was actually already working! I was running a test project and hadn't bothered rendering the video branch. I mistakenly thought VSFilter would display the file name as it does when it picks up an SRT with the same name as the video. Apparently it doesn't when you set the file name through the interface. So instead of checking the filter's property page in my graph in GraphEdit, I should simply have rendered the video and I'd have seen the subtitles in the video window.

Related

VB.NET 2010 - Extracting an application resource to the Desktop

I am trying to extract an application resource from My.Resources.FILE
I have discovered how to do this with DLL & EXE files, but I still need help with the code for extracting PNG & ICO files.
Other file types also. (If possible)
Here is my current code that works with DLL & EXE files.
Dim File01 As System.IO.FileStream = New System.IO.FileStream("C:\Users\" + Environment.UserName + "\Desktop\" + "SAMPLE.EXE", IO.FileMode.Create)
File01.Write(My.Resources.SAMPLE, 0, My.Resources.SAMPLE.Length)
File01.Close()
First things first, the code you have is bad. When using My.Resources, every time you use a property, you extract a new copy of the data. That means that your second line is getting the data to write twice, with the second time being only to get its length. At the very least, you should be getting the data only once and assigning it to a variable, then using that variable twice. You should also be using a Using statement to create and destroy the FileStream. Even better though, just call File.WriteAllBytes, which means that you don't have to create your own FileStream or know the length of the data to write. You should also not be constructing the file path that way.
Dim filePath = Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "SAMPLE.EXE")
File.WriteAllBytes(filePath, My.Resources.SAMPLE)
As for your question, the important thing to understand here is that it really has nothing to do with resources. The question is really how to save data of any particular type and that is something that you can look up for yourself. When you get the value of a property from My.Resources, the type of the data you get will depend on the type of the file you embedded in first place. In the case of a binary file, e.g. DLL or EXE, you will get back a Byte array and so you save that data to a file in the same way as you would any other Byte array. In the case of an image file, e.g. PNG, you will get back an Image object, so you save that like you would any other Image object, e.g.
Dim filePath = Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "PICTURE.PNG")
Using picture = My.Resources.PICTURE
picture.Save(filePath, picture.RawFormat)
End Using
For an ICO file you will get back an Icon object. I'll leave it to you to research how to save an Icon object to a file.
EDIT:
It's important to identify what the actual problem is that you're trying to solve. You can obviously get an object from My.Resources so that is not the problem. You need to determine what type that object is and determine how to save an object of that type. How to do that will be the same no matter where that object comes from, so the resources part is irrelevant. Think about what it is that you have to do and write a method to do it, then call that method.
In your original case, you could start like this:
Dim data = My.Resources.SAMPLE
Once you have written that - even as you write it - Intellisense will tell you that the data is a Byte array. Your actual problem is now how to save a Byte array to a file, so write a method that does that:
Private Sub SaveToFile(data As Byte(), filePath As String)
'...
End Sub
You can now which you want to do first: write code to call that method as appropriate for your current scenario or write the implementation of the method. There are various specific ways to save binary data, i.e. a Byte array, to a file but, as I said, the simplest is File.WriteAllBytes:
Private Sub SaveToFile(data As Byte(), filePath As String)
File.WriteAllBytes(filePath, data)
End Sub
As for calling the method, you need to data, which you already have, and the file path:
Dim data = My.Resources.SAMPLE
Dim folderPath = My.Computer.FileSystem.SpecialDirectories.Desktop
Dim fileName = "SAMPLE.EXE"
Dim filePath = Path.Combine(folderPath, fileName)
SaveToFile(data, filePath)
Simple enough. You need to follow the same steps for any other resource. If you embedded a PNG file then you would find that the data is an Image object or, more specifically, a Bitmap. Your task is then to learn how to save such an object to a file. It shouldn't take you long to find out that the Image class has its own Save method, so you would use that in your method:
Private Sub SaveToFile(data As Image, filePath As String)
data.Save(filePath, data.RawFormat)
End Sub
The code to call the method is basically as before, with the exception that an image object needs to be disposed when you're done with it:
Dim data = My.Resources.PICTURE
Dim folderPath = My.Computer.FileSystem.SpecialDirectories.Desktop
Dim fileName = "SAMPLE.EXE"
Dim filePath = Path.Combine(folderPath, fileName)
SaveToFile(data, filePath)
data.Dispose()
The proper way to create and dispose an object in a narrow scope like this is with a Using block:
Dim folderPath = My.Computer.FileSystem.SpecialDirectories.Desktop
Dim fileName = "SAMPLE.EXE"
Dim filePath = Path.Combine(folderPath, fileName)
Using data = My.Resources.PICTURE
SaveToFile(data, filePath)
End Using
Now it is up to you to carry out the same steps for an ICO file. If you are a hands on learner then get your hands on.

Dictionary comes back from cache as Null/Nothing

I'm writing some vb.net code to retrieve some data from middleware, do some stuff with it, and then save a dictionary of name/value pairs to cache for quick retrieval when needed.
It all seems to go well until I retrieve the object from cache and it is always Null/Nothing.
Yet in the watch window I can see my cached object and it has data in it.
Thinking maybe it's a serialization issue I made a new class that inherits Dictionary and is marked as serializable but I still get the same results.
So right now I have this class:
<Serializable()> Public Class SerialDict
Inherits Dictionary(Of String, String)
Public Sub New()
End Sub
End Class
I populate it and put it into cache like this:
Dim Licenses As New SerialDict
For Each r As DataRow In dtLicenses.Rows
Dim prikey As String = r("SettingID").ToString.Trim
Dim decryptionKey As String = GetHash((xx))
Dim licData As String = DecryptData(r("SettingVal"), decryptionKey)
Licenses.Add(r("SettingKey"), licData)
Next
If IsNothing(HttpContext.Current.Cache("Licenses")) Then
HttpContext.Current.Cache.Add("Licences", Licenses, Nothing, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Default, Nothing)
End If
Then elsewhere we need to check that data so I try to retrieve it like this:
Dim Licences As SerialDict = CType(HttpContext.Current.Cache("Licenses"), SerialDict)
At this point Licenses is always Nothing, but the watch window shows data in HttpContext.Current.Cache("Licenses").
Any suggestions? Thanks!

SvgDocument.Draw() Object reference not set to an instance of an Object

I have looked around Stackoverflow and the internet in generel, but haven't found a post that could help me solve my problem.
My problem is that in the following code snippet at line
Dim bm As Bitmap = SvgDoc.Draw()
I get an Object reference not set to an instance of an object.
Protected Function SvgToPng(ByVal svg As String) As Byte()
svg = svg.Replace("url(""#lineArea"")", "url('#lineArea')")
Dim byteArray = Encoding.UTF8.GetBytes(svg)
Dim str As New MemoryStream(byteArray)
Dim svgDoc = SvgDocument.Open(str)
scaleSvgDoc(svgDoc, 7)
Dim bm As Bitmap = svgDoc.Draw()
Dim out As New MemoryStream
bm.Save(out, ImageFormat.Png)
Return out.ToArray
End Function
I have multiple buttons, under different menus that access this method. My problem is that for a single of these menus, I get the problem as described above, but I don't get it for the rest.
I have checked that both the SvgDoc, str and byteArray all are set, and the only difference between the working one, and one that doesn't work, is the SvgString (in this case svg).
Anyone that can help me here?
EDIT: It's the SVG Rendering Engine library that I use.
I don't know much about this library, I assume you are using SVG Rendering Engine? But I noticed that there is a method SvgDocument.OpenAsBitmap. Why not just open as a bitmap and then change to whatever image format you want?

Creating a cross domain web service

So, I am very new to creating web services, but have successfully managed to make a simple webservice which returns information as I'd need from a database as List(Of dictionary(of string, string)) object.
For the purpose of testing, I have created this manually, my code looks like this:
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Web.Script.Serialization
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class test
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function dic() As String
Dim newDic As New List(Of Dictionary(Of String, String))
Dim one As New Dictionary(Of String, String)
one.Add("id", "1")
one.Add("name", "the name")
one.Add("price", "5.99")
newDic.Add(one)
Dim two As New Dictionary(Of String, String)
two.Add("id", "2")
two.Add("name", "item name two")
two.Add("price", "1299")
newDic.Add(two)
Dim s As New JavaScriptSerializer
Dim str As String = s.Serialize(newDic)
Return str
End Function
End Class
This webservice "dic" gives me serialized string/list looking like this:
[{"id":"1","name":"the name","price":"5.99"},{"id":"2","name":"item name two","price":"1299"}]
I can read this in VB code like this:
Sub loadMe() Handles Me.Load
Dim t As New websvce.testSoapClient
Dim d As String = t.dic
Dim s As New JavaScriptSerializer
Dim d2 = s.DeserializeObject(d)
Response.Write(d2(1)("name") & "<hr>")
End Sub
which gives me the output of the "name" element with index "1". It works fine.
However, unsurprisingly, it does not work when trying to grab this info with jQuery using code as follows:
$(document).ready(function () {
$.getJSON('URL/test.asmx/dic', function (data) {
alert(data);
});
});
So I have spent a good part of the day Googling this, and found all kinds of comments and conversations telling me about why cross domain scripting is not allowed and that there are ways round it using Proxies, adding headers to pages and so on... but... no conclusive solution to the problem.
Here's some of the SO questions I have found:
Origin http://localhost is not allowed by Access-Control-Allow-Origin
How to implement "Access-Control-Allow-Origin" header in asp.net
There's many more, but I cannot find an absolute solution, which fits my criteria. I want to output data in a format similar to that above, and access it from another domain with jQuery.
The easiest work around I have found is to write a handling page which runs on the receiving server, simply loading the data as per my VB code example above and operating as a webMethod on that server to spit out JSON, but I'd rather the whole thing could be handling by the web service, thus no need to run extra files on the consuming server.
EDIT; actually, I do also need to be able to perform POST operations.

Restrict type in a Collection inside a class module

I have a collection inside a class module. I'd like to restrict the object type that is "addable" to this collection, i.e. collection should only ever accept objects of one given type and nothing else.
Is there any way to enforce the type of objects added to a collection?
From what I can tell, there is no built-in way to do this. Is the solution then to make this collection private, and build wrapper functions for the methods usually accessible for Collections, i.e. Add, Remove, Item, and Count?
I kinda hate having to write 3 wrapper functions that add no functionality, just to be able to add some type enforcement to the Add method. But if that's the only way, then that's the only way.
There is no way to avoid wrapper functions. That's just inherent in the "specialization through containment/delegation" model that VBA uses.
You can build a "custom collection class", though. You can even make it iterable with For...Each, but that requires leaving the VBA IDE and editing source files directly.
First, see the "Creating Your Own Collection Classes" section of the old Visual Basic 6.0 Programmer's Guide:
http://msdn.microsoft.com/en-us/library/aa262340(v=VS.60).aspx
There is also an answer here on stackoverflow that describes the same thing:
vb6 equivalent to list<someclass>
However, those are written for VB6, not VBA. In VBA you can't do the "procedure attributes" part in the IDE. You have to export the class module as text and add it in with a text editor. Dick Kusleika's website Daily Dose of Excel (Dick is a regular stackoverflow contributer as you probably know) has a post from Rob van Gelder showing how to do this:
http://www.dailydoseofexcel.com/archives/2010/07/04/custom-collection-class/
In your case, going to all that trouble - each "custom collection" class needs its own module - might not be worth it. (If you only have one use for this and it is buried in another class, you might find that you don't want to expose all of the functionality of Collection anyway.)
This is what I did. I liked Rob van Gelder's example, as pointed to by #jtolle, but why should I be content with making a "custom collection class" that will only accept one specific object type (e.g. People), forever? As #jtolle points out, this is super annoying.
Instead, I generalized the idea and made a new class called UniformCollection that can contain any data type -- as long as all items are of the same type in any given instance of UniformCollection.
I added a private Variant that is a placeholder for the data type that a given instance of UniformCollection can contain.
Private mvarPrototype As Variant
After making an instance of UniformCollection and before using it, it must be initialized by specifying which data type it will contain.
Public Sub Initialize(Prototype As Variant)
If VarType(Prototype) = vbEmpty Or VarType(Prototype) = vbNull Then
Err.Raise Number:=ERR__CANT_INITIALIZE, _
Source:=TypeName(Me), _
Description:=ErrorDescription(ERR__CANT_INITIALIZE) & _
TypeName(Prototype)
End If
' Clear anything already in collection.
Set mUniformCollection = New Collection
If VarType(Prototype) = vbObject Or VarType(Prototype) = vbDataObject Then
' It's an object. Need Set.
Set mvarPrototype = Prototype
Else
' It's not an object.
mvarPrototype = Prototype
End If
' Collection will now accept only items of same type as Prototype.
End Sub
The Add method will then only accept new items that are of the same type as Prototype (be it an object or a primitive variable... haven't tested with UDTs yet).
Public Sub Add(NewItem As Variant)
If VarType(mvarPrototype) = vbEmpty Then
Err.Raise Number:=ERR__NOT_INITIALIZED, _
Source:=TypeName(Me), _
Description:=ErrorDescription(ERR__NOT_INITIALIZED)
ElseIf Not TypeName(NewItem) = TypeName(mvarPrototype) Then
Err.Raise Number:=ERR__INVALID_TYPE, _
Source:=TypeName(Me), _
Description:=ErrorDescription(ERR__INVALID_TYPE) & _
TypeName(mvarPrototype) & "."
Else
' Object is of correct type. Accept it.
' Do nothing.
End If
mUniformCollection.Add NewItem
End Sub
The rest is pretty much the same as in the example (plus some error handling). Too bad RvG didn't go the whole way! Even more too bad that Microsoft didn't include this kind of thing as a built-in feature...
I did almost the same code of Jean-François Corbett, but I adapted because for some reason wasn't working.
Option Explicit
Public pParametro As String
Private pColecao As New Collection
Public Sub Inicializar(ByVal parametro As String)
pParametro = parametro
End Sub
Public Sub Add(NewItem As Object)
If TypeName(NewItem) <> pParametro Then
MsgBox "Classe do objeto não é compatível à coleção"
Else
pColecao.Add NewItem
End If
End Sub
Public Property Get Count() As Long
Count = pColecao.Count
End Property
Public Property Get Item(NameOrNumber As Variant) As Variant
Set Item = pColecao(NameOrNumber)
End Property
Sub Remove(NameOrNumber As Variant)
pColecao.Remove NameOrNumber
End Sub
Then, when i want to create an instance from CCollection I make like the code bellow:
Set pFornecedores = New CCollection
pFornecedores.Inicializar ("CEmpresa")
Where CEmpresa is the class type from the object I want
Yes. The solution is to make your collection private and then make public wrapper functions to add, remove, getitem and count etc.
It may seem like hassle to write the additional code but it is a more robust solution to encapsulate the collection like this.