Using TM1api.dll in C#

Post Reply
argnur
Posts: 2
Joined: Sat Jan 31, 2009 10:52 pm

Using TM1api.dll in C#

Post by argnur »

Hi people,
I have a problem with using tm1api.dll in C# application: when I check if the connection to the server was successful TM1ValType(hUser, vServerObject) == TM1ValTypeObject() I always get false(TM1ValType returns TM1ValTypeError). It is kinda urgent, so I will be very grateful if someone helps.

Here is the code:

Code: Select all

            TM1APIInitialize();
            int hUser = TM1SystemOpen();
            string st = "COMP"; // the name of the computer
            TM1SystemAdminHostSet(hUser, st);
            int hPool1 = TM1ValPoolCreate(hUser);       

            int hPool2 = TM1ValPoolCreate(hUser);

            string sUserName = "admin"; //!!!!! I "start local server" via TM1 Architect and use default names and password
            string sServerName = "local"; //!!!!! Is it OK?
            string sPassword = "";

            int vStringLength = 0; //!!!!! in C++ examples it is given as 10 and in VB as 0

            int vUserName = TM1ValString(hPool1, sUserName, vStringLength);
            int vServerName = TM1ValString(hPool1, sServerName, vStringLength);
            int vPassword = TM1ValString(hPool1, sPassword, vStringLength);
            
            int vServerObject = TM1SystemServerConnect(hPool1, vServerName, vUserName, vPassword);
            
            if (TM1ValType(hUser, vServerObject) == TM1ValTypeObject()){
                Console.WriteLine("Success");
            }
            if (TM1ValType(hUser, vServerObject) == TM1ValTypeError()){
                Console.WriteLine("DAMN!!!"); // !!!!! I get this
            }
Here are the API functions' declarations:

Code: Select all

        [DllImport(@"tm1api.dll")]
        public static extern void TM1APIInitialize();

        [DllImport(@"tm1api.dll")]
        public static extern int TM1SystemOpen();

        [DllImport(@"tm1api.dll")]
        public static extern void TM1SystemAdminHostSet(int hUser, [MarshalAs(UnmanagedType.LPStr)]string sAdminHost);

        [DllImport(@"tm1api.dll")]
        public static extern int TM1ValPoolCreate(int User);

        [DllImport(@"tm1api.dll")]
        public static extern int TM1ValString(int hPool, [MarshalAs(UnmanagedType.LPStr)]string st, int maxSize);

        [DllImport(@"tm1api.dll")]
        public static extern int TM1SystemServerConnect(int hPool, int sServerName, int sClientName, int sPassword);

        [DllImport(@"tm1api.dll")]
        public static extern int TM1ValType(int hUser, int vServerObject);

        [DllImport(@"tm1api.dll")]
        public static extern int TM1ValTypeObject();

        [DllImport(@"tm1api.dll")]
        public static extern int TM1ValTypeError();

Please reply if you see my mistakes or if you have sample C# code using tm1api.dll
Thanks in advance!
User avatar
Renaud MARTIAL
Posts: 25
Joined: Thu May 15, 2008 10:18 am
Location: Paris, France

Re: Using TM1api.dll in C#

Post by Renaud MARTIAL »

Hello,

the TM1SystemServerConnect() function doesn't return a handle to an objet (ie. a TM1 object), but a handle to the server.
Thus, you don't have to check if it's an object.

The only test required is (as you do) to check that it is different of TM1ValTypeError().
If it is different, you can retrieve the Error code and message using TM1ValErrorCode( hUser, vServerObject) and TM1ValErrorString( hUser, vServerObject) functions.

Regards,

Renaud.
argnur
Posts: 2
Joined: Sat Jan 31, 2009 10:52 pm

Re: Using TM1api.dll in C#

Post by argnur »

Hi Renaud,
Thanks for the reply. TM1ValType(hUser, vServerObject) returns TM1ValTypeError(), so as you suggested I tried to get the error string:
if (TM1ValType(hUser, vServerObject) == TM1ValTypeError()){
int code = TM1ValErrorCode(hUser, vServerObject);
IntPtr intptr = TM1ValErrorString(hUser, code); // AccessViolationException here
String str = Marshal.PtrToStringAnsi(intptr);
}
but I get only AccessViolationException. Probably there is a mistake in getting the string error. Can you please help me to fix it?
User avatar
Renaud MARTIAL
Posts: 25
Joined: Thu May 15, 2008 10:18 am
Location: Paris, France

Re: Using TM1api.dll in C#

Post by Renaud MARTIAL »

Hi,

the TM1V for which you ask the string value has to be the one that is of type 'TM1ValTypeError'.
So, the correct code has to be something like this:

if( TM1ValType(hUser, vServerObject) == TM1ValTypeError()){
int nErrCode = TM1ValErrorCode(hUser, vServerObject);
char *szErrMsg = TM1ValErrorString(hUser, vServerObject);
cout << "error " << nErrCode << ":" << szErrMsg;
}

Regards,

Renaud.
B.M. Beltman
Posts: 3
Joined: Tue Jul 26, 2011 8:36 pm
OLAP Product: TM1
Version: 9.5.1
Excel Version: 2007

Re: Using TM1api.dll in C#

Post by B.M. Beltman »

Hi People,

I am looking for the full set of API function declarations. All help appreciated!

Thanks,

Bastiaan Beltman
Alan Kirk
Site Admin
Posts: 6643
Joined: Sun May 11, 2008 2:30 am
OLAP Product: TM1
Version: PA2.0.9.18 Classic NO PAW!
Excel Version: 2013 and Office 365
Location: Sydney, Australia
Contact:

Re: Using TM1api.dll in C#

Post by Alan Kirk »

B.M. Beltman wrote: I am looking for the full set of API function declarations. All help appreciated!
Which API? There are three of them. Four, if you count macro functions in Excel.

Since you've posted it in this thread you're presumably referring to the .Net API. Go to the folder in which you installed TM1 (typically C:\Program Files\Cognos\TM1), look in the API folder, then the DOTNETAPIDOC folder. There's a .chm help file there which contains all of the functions.

If you haven't used the .Net API before, be prepared to be in for a helluva disappointment. It will let you read and write values from and to a cube. It will let you do some other rudimentary end user tasks. It will not let you handle most of the metadata functions that administrators need to do. It is, IMHO, a waste of space in its present form.
"To them, equipment failure is terrifying. To me, it’s 'Tuesday.' "
-----------
Before posting, please check the documentation, the FAQ, the Search function and FOR THE LOVE OF GLUB the Request Guidelines.
B.M. Beltman
Posts: 3
Joined: Tue Jul 26, 2011 8:36 pm
OLAP Product: TM1
Version: 9.5.1
Excel Version: 2007

Re: Using TM1api.dll in C#

Post by B.M. Beltman »

Hi,

Let me clarify my question.

I want to use the TM1api.dll in a C# program. For this one needs to declare alle functions in this way:

[DllImport(@"tm1api.dll")]
public static extern void TM1APIInitialize();

[DllImport(@"tm1api.dll")]
public static extern int TM1SystemOpen();

....

I would be helped in a great way if someone has the full listing of these function declarations.

I want to be able to to handle metadata functions. This is why I do not want to use the .net API (this is indeed limited, as I found out also) but the tm1API.dll.

Thanks,

Bastiaan
Alan Kirk
Site Admin
Posts: 6643
Joined: Sun May 11, 2008 2:30 am
OLAP Product: TM1
Version: PA2.0.9.18 Classic NO PAW!
Excel Version: 2013 and Office 365
Location: Sydney, Australia
Contact:

Re: Using TM1api.dll in C#

Post by Alan Kirk »

B.M. Beltman wrote:Hi,

Let me clarify my question.

I want to use the TM1api.dll in a C# program. For this one needs to declare alle functions in this way:

[DllImport(@"tm1api.dll")]
public static extern void TM1APIInitialize();

[DllImport(@"tm1api.dll")]
public static extern int TM1SystemOpen();

....

I would be helped in a great way if someone has the full listing of these function declarations.

I want to be able to to handle metadata functions. This is why I do not want to use the .net API (this is indeed limited, as I found out also) but the tm1API.dll.

Thanks,

Bastiaan
I've already logged a gripe with Cognos about the fact that the functions are not fully documented in the TM1 API Manual (which should nonetheless be your first port of call). They may get around to fixing that, some day. In the meantime I suggest that you go to C:\Program Files\Cognos\TM1\API\TM1API and look at the file Tm1api.bas or TM1api.h. Yes, they're VBA / C files respectively, but they do contain most of the function declarations which you should be able to translate to your own language. (Including some which are missing from the manual.) I'd be surprised if anyone has already done that translation for C# though... but you never know, you might get lucky.
"To them, equipment failure is terrifying. To me, it’s 'Tuesday.' "
-----------
Before posting, please check the documentation, the FAQ, the Search function and FOR THE LOVE OF GLUB the Request Guidelines.
bagovino
Posts: 26
Joined: Fri Oct 09, 2009 5:54 pm
OLAP Product: TM1
Version: 9.5.1
Excel Version: 2007

Re: Using TM1api.dll in C#

Post by bagovino »

Alan Kirk wrote:I'd be surprised if anyone has already done that translation for C# though... but you never know, you might get lucky.
Here you go:

https://github.com/bagovino/TM1ProcessR ... /TM1API.cs

Everything is there except for the functions that deal with the TM1Object struct. Please note that I haven't fully tested every single function and that this comes with the usual this is provided as-is and that you use at your own risk warnings.
B.M. Beltman
Posts: 3
Joined: Tue Jul 26, 2011 8:36 pm
OLAP Product: TM1
Version: 9.5.1
Excel Version: 2007

Re: Using TM1api.dll in C#

Post by B.M. Beltman »

bagovino wrote:
Alan Kirk wrote:I'd be surprised if anyone has already done that translation for C# though... but you never know, you might get lucky.
Here you go:

https://github.com/bagovino/TM1ProcessR ... /TM1API.cs

Everything is there except for the functions that deal with the TM1Object struct. Please note that I haven't fully tested every single function and that this comes with the usual this is provided as-is and that you use at your own risk warnings.
Super! Just what I needed. Many, many thanks for sharing this!!!

Bastiaan
bagovino
Posts: 26
Joined: Fri Oct 09, 2009 5:54 pm
OLAP Product: TM1
Version: 9.5.1
Excel Version: 2007

Re: Using TM1api.dll in C#

Post by bagovino »

You're welcome. :) If you encounter any problems with it let me know; I've only really used the process and chore functions. I also have a translation of it for Python for the extremely curious.
Kyro
Community Contributor
Posts: 126
Joined: Tue Nov 03, 2009 7:46 pm
OLAP Product: MODLR - The CPM Cloud
Version: Always the latest.
Excel Version: 365
Location: Sydney, Australia
Contact:

Re: Using TM1api.dll in C#

Post by Kyro »

Hey, Just though I should share this - in the C# Class you have this:

Code: Select all

            [DllImport(TM1APIDLL)]
            public static extern IntPtr TM1ValIndex(IntPtr hUser, int iIndex);
But the original C++ API has this instead:

Code: Select all

TM1IMPORT TM1V TM1API TM1ValIndex( TM1P hPool, TM1_INDEX InitIndex ); 
It will have no effect on how the function runs but if developers feed in a hUser instead of a Value Pool it's not the most fun to debug - I know because I did.
bagovino
Posts: 26
Joined: Fri Oct 09, 2009 5:54 pm
OLAP Product: TM1
Version: 9.5.1
Excel Version: 2007

Re: Using TM1api.dll in C#

Post by bagovino »

Thanks, I'll fix that. I went into autopilot typing most of that out. :D

Another thing that I discovered that you'll probably want to change is that for every external function declaration that returns a StringBuilder make it return an IntPtr instead. Returning a StringBuilder worked in my 32-bit code, but not when I tried to compile it for 64-bit. Once you do that to get any string value from an IntPtr you would just use Marshal.PtrToStringAnsi(strVal).

For example:

Code: Select all

[DllImport(TM1APIDLL)]
public static extern StringBuilder TM1SystemServerName(IntPtr hUser, int index);
Should be:

Code: Select all

[DllImport(TM1APIDLL)]
public static extern IntPtr TM1SystemServerName(IntPtr hUser, int index);
Here's an usage example:

Code: Select all

numServers = TM1API.SystemFunctions.TM1SystemServerNof(userHandle);

for(int count = 1; count <= numServers; count++)
{
    serverNamePtr = TM1API.SystemFunctions.TM1SystemServerName(userHandle, count);
    serverName = Marshal.PtrToStringAnsi(serverNamePtr);
}
DHiggins
Posts: 3
Joined: Fri May 23, 2008 9:59 pm

Re: Using TM1api.dll in C#

Post by DHiggins »

bagovino wrote:I also have a translation of it for Python for the extremely curious.
I am interested in the Python translation. Thanks!
Post Reply