Click here to Skip to main content
15,112,453 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I do have a MSSQL table in which a column contains numerous OLE objects.
Contents are various types and versions of MS Office documents + TIF Images (Typical office stuff).
Case: Need of an application that can handle those documents (if required applications are installed) and some other tasks.
My thoughts:
1- Pop objects - rip ole struct according to Windows Compound Binary File Format Specification[^] - push back raw files - add a type field to the table - perform other tasks.
But automation hell arises.
2- Make a .dll in an older IDE which has OleConteiner - somehow pass OLE to dll - take it back if and when done - perform other tasks.
But... I don't really know how many things can go wrong.

The question is, what should I do? Is there a feasible way to handle OLE objects?

Progress Update:
1- Rip ole:
public void ReadnDump(System.IO.Stream p_Stream,string p_FileName)
    System.IO.BinaryReader br = new System.IO.BinaryReader(p_Stream);
    m_head = new Header();
    m_DirEntities = new System.Collections.Generic.List<DirectoryEntry>();
    m_FAT = new System.Collections.Generic.List<UInt32>();
    m_DiFAT = new System.Collections.Generic.List<UInt32>();
    m_MiniFAT = new System.Collections.Generic.List<UInt32>();
    m_MiniStream = new System.IO.MemoryStream();
    m_DirDictonary = new System.Collections.Generic.Dictionary<string,DirectoryEntry>();
    byte[] buffer;
    // Binary reading stuff below explained in link above
    readHeader(ref br);
    readFAT(ref br);
    readDirectoryEntities(ref br);
    readMiniFat(ref br);
    string fileName;
    string className = readCompObjClassName();
    UInt32 sect;
    int fileSize;
    System.IO.FileStream fw;
    switch (className)
        // Some cases...
        case "Word.Document.12":
            sect = m_DirDictonary["\\Root Entry\\Package"]._sectStart;
            // Dump contents to file named "p_FileName" + decided extention...
        // Some more cases...

Automation... I hope it does not come to this.

2- Managed to write a Delphi dll and open ole but... Event sink, callbacks waiting for me yay!
Updated 29-Apr-11 6:27am
Manfred Rudolf Bihy 28-Apr-11 14:02pm
Sounds like an interesting question to me. Let's see what the seasoned OLE specialists aboard CP have to say to this.
Rajesh Lagaria 28-Apr-11 20:22pm
send your code ! what you do
yesotaso 28-Apr-11 22:10pm
Function OleTest(p1:PChar): Boolean; StdCall;
var ole1:TOleContainer;
OleTest:= False;
ole1 := TOleContainer.CreateParented(0);
fstr := TFileStream.Create(p1,fmRead);
..... Test dll
Manfred Rudolf Bihy 29-Apr-11 8:38am
How come you replied to my comment? I do hope this was done by mistake ;)
yesotaso 29-Apr-11 11:04am
:) Mistake indeed.

1 solution

Ok I came up with solution as:

Delphi side: Created Dll with 6 exports and a little factory inside.

C# side:

//Delegate for dll callbacks
public delegate void ProcDelegate(int value,int callerID);
private static ProcDelegate procDelegate;
// Declarations for exports
[DllImport("OleWrapper.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
public static extern void InitOle();
// And 5 more for each exported functions

// Delegated function
// value is an arbitrary number I decided to identify operation
// callerID is the container ID which is returned by OleOpen or OleCreateFromFile
public void DelphiCallBack(int value, int callerID)
    int index;
    switch (value)
         case 1:
             // Close
             // this creates a temporary file to be inserted to DB as it is
             // tag and bag C# side of open item list
         case 2:
             // Datachanged - some debug stuff. Just ignore
         case 3:
             // Save
             // User pressed saved the open document.
             // handle created temporary file
         case 4:
             // Renamed - some debug stuff. Just ignore
         case 5:
             // View changed - some debug stuff. Just ignore

Init and RegisterCallback called before doing anything else CleanUp destroys the factory.
Only problem is I tried to avoid memory contact with dll so that I used temporary files to pass OLE objects back and forth. Any further suggestion is welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900