Resolved Having problems with Shell32

MPIon

Well-known member
Joined
Jun 13, 2020
Messages
73
Location
England
Programming Experience
10+
I am trying to get Jpeg metadata using Shell32, as per various posted examples.
I have added the appropriate reference Microsoft Shell Controls and Automation.
The line [STATThread] exists before Main.
The program compiles, but will not run. I get the following error :-
1691099717768.png

Can't figure out what is going on here or even what this means.
Can anyone shed any light on this - must be something pretty obvious?
 
Only thing I can think of that you're calling this from a different thread that is not set as ApartmentState.STA.
 
Show us how you declared your Shell32 class as well as your implementation of Shell32.Shell().
 
Show us how you declared your Shell32 class as well as your implementation of Shell32.Shell().

That's from the COM reference.
 
Only thing I can think of that you're calling this from a different thread that is not set as ApartmentState.STA.

Thanks for the reply. I have two threads - main and background. Both have the [STATThread] declared :-
[STAThread]
private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
......
Shell32.Shell shell = new Shell32.Shell();
etc.
I am calling the Shell32 from the background thread.
Must admit, I am a bit rusty on c# and have not really understood what [STATThread] is all about or why it is necessary. Will have to do a lot more research.
 
You can't make a BackgroundWorker event handler a STA thread. You must create the new Thread yourself, set IsBackground true, call SetApartmentState and Start the thread.
 
That's from the COM reference.

Thanks for the reply. All I did was add a project reference :-
1691136975948.png

This then shows :-
1691137022531.png


The code does compile, so it can find Shell32, which it did not before I added the project reference.

Actually, I would prefer not to use the Shell32 dll and instead use some standard library in Visual Studio to access the jpeg metadata such as Camera Maker and Model.
Other people seem to have done it using System.Drawing.Image, but spend yesterday trying to get that to work without success. Not sure if that is incredibly slow though, if it has to load the image before getting the metadata using PropertyItem.
 
You can't make a BackgroundWorker event handler a STA thread. You must create the new Thread yourself, set IsBackground true, call SetApartmentState and Start the thread.

Ok, thanks for the reply. That's probably it, but don't understand why yet. I am missing a lot of knowledge here. Don't really see the difference between using a background thread and starting a background thread my self. I'll have to go away and do some more study on this.

No, I don't think that was it. I removed the BackgroundWorker, created a new thread, set IsBackground true, call SetApartmentState and Started the thread.
The thread ran and encountered exactly the same error as before with the shell command Shell32.Shell shell = new Shell32.Shell();
 
Last edited:
OK, got this to work now. Had to use thread.SetApartmentState(ApartmentState.STA) rather than just [STAThread] - thought they did the same thing.
i.e.
Thread thread = new Thread(new ThreadStart(Worker));
thread.IsBackground = true;
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
So, I've got past the Shell32 assigment without error - now on to the next problem getting the attributes.

I'll mark this particular problem as solved.

Thanks for the help
 
Had to use thread.SetApartmentState(ApartmentState.STA) rather than just [STAThread] - thought they did the same thing.
Apply this attribute to the entry point method (the Main() method in C# and Visual Basic). It has no effect on other methods.
This also means it will only apply to the main thread.
 

Latest posts

Back
Top Bottom