OP.XmlPrettifier

Background

In my recent project I had automatically generated XML log files which were pretty condensed to preserve space. Parsing them with an application is not a problem but sometimes I needed to read them and this cause issues since it was nearly impossible to read it with Notepad++.
There are some online Xml beautifiers but our log files were 100+MB large so it was not practical to use them.
Also the log files were XML fragments and not complete documents so it caused problems with some desktop beautifier applications.
At the end I decided to create my own small console application.

How it works

OP.XmlPrettifier is a .NET 3.5 C# console application. It uses the Xml reader to read the fragmented/complete Xml and then loads fragment by fragment into XmlDocument and uses formatting XmlWriter to output the same XML indented.
When invoked with one parameter it reads the input file and outputs the result to the console. If two parameters are present then it reads the input file and outputs the result to the output file.
I’ve hosted the application on codeplex and you can find it here: https://xmlprettifier.codeplex.com/

Created small note taking application – introducing OP.Notes

I have spent few days to create a small note taking application as I really needed something more then just text editor (Notepad++ is a fine application) to add notes, have simple TODO lists etc. I’ve placed the app on Codeplex as other people might find it usefull as well: OP.Notes. Here is a screenshot of how it looks like:

OP.Notes

Tagged with: , ,

How to determine which application is stealing focus

Recently I’ve had an issue that every 15 minute whatever app I was working on would loose a focus for a few seconds. Obviously some another process window was stealing the focus but the problem was that this another window was not visible and I got the focus back after few seconds. Additionally this also prevented my laptop from going to sleep and even starting the screensaver.

After googling around a bit I find a post from the Matt Gertz It’s Elementary: Using VB To Get Process Information. It describes a way to create an application which prints out information about the current foreground window and the owning process once the application loses focus.

After creating such a small application and leaving it running for a while I found that my mobile telecom Internet USB stick update program was running every 15 minutes (T-Mobile Internet Manager / LiveUpdate / ouc.exe). After that it was easy to find where the application was being started by using the fantastic Sysinternals Autoruns application and turning it off.

I’ve attached 32 bit and 64 bit versions of the application. First try to run with 32 bit version and if you get an error that 32 bit application can not read 64 bit process information then run the 64 bit application.

Focus stealer 32 bit
Focus stealer 64 bit

Tagged with: ,

Custom Windows Forms localization

Recently while working on a customer project I had a request to provide ability to enable the end user to customize the labels and captions on Form(s) and UserControl(s). In general this is not a hard thing to achieve but it makes the developers life harder since you need some way to track/mark labels and text control and later on in code provide some sort of a mechanism to replace the text that developer entered in Visual Studio Forms designer with text provided by the end user.

I’ve decided to create a small library for that which you can find at Codeplex: Windows Forms custom localizer

Basic concepts

While thinking about such a solution I realized that Visual Studio forms designer already provides such a functionality. Inside Visual Studio Forms designer each root component (Form or UserControl) gets a custom boolean property called Localizable. When set to True this property changes the way that Visual Studio serializes the controls into the appropriate .designer.cs/vb file.

It creates a ComponentResourceManager inside the InitializeComponent method and then uses this resource manager is to provide values for all properties of components which are marked with <Localizable> attribute
Example:

Private Sub InitializeComponent()
    Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(MainForm))
    '
    'btnCancel
    '
    resources.ApplyResources(Me.btnCancel, "btnCancel")
    Me.btnCancel.Name = "btnCancel"
    Me.btnCancel.UseVisualStyleBackColor = True
    '
    'Label1
    '
    resources.ApplyResources(Me.Label1, "Label1")
    Me.Label1.Name = "Label1"
    '
    'TextBox1
    '
    resources.ApplyResources(Me.TextBox1, "TextBox1")
    Me.TextBox1.Name = "TextBox1"
    ...

This got me thinking. The resource manager does exactly what I would need to do and if I could somehow replace it with my own custom resource manager then I could achieve everything I wanted. I could even handle 3rd party controls because if 3rd party control supports localization then my custom resource manager could provide custom text for it as well. Also if I can get the previous resource manager which was generated by VS Forms designer I could retain the existing functionality of the resource manager which is to provide localization based on satellite resource assemblies and localization of non-text properties. (If you take a closer look at the Visual Studio ComponentResourceManager you will see that it not only provides text for String properties of the component but it can also localize additional properties such as Visible and Size. This however I chose to ignore that functionality since the original request that I had was just to enable the end user to change captions and label and not to change size or visibility of the control.)

Visual Studio Forms designer challenge

This all looked good while thinking about it but now I had to put my thinking into code. In essence I had to somehow get into the VS Form designer code generation and instead of instantiating the .NET standard ComponentResourceManager I had to get it to instantiate my own LocalizerComponentResourceManager.

First thing I decided to do is to encapsulate this functionality into a Component derived type which I called LocalizerComponent. My reasoning was that this would enable me to just drop the component to UserControl or Form and have it work its magic automatically. No need to have my (your) code derive from a specific base class if that is in anyway possible.

Creating the component itself was easy. But how to make it replace code generated by VS? Specifically the code generated inside the InitializeComponent() method? As it turns out there is way to have the component affect the generated code. You need to provide your own serializer for your component. This can be done by attaching the DesignerSerializerAttribute to your class. For LocalizerComponent I’ve created LocalizerComponentSerializer.

Once you have created your own serializer you have two methods that you need to override: – Serialize which enables you to provide CodeDom statements which represent serialized data from your component – Deserialize which enables you to consume the CodeDom statements intended for your component

Once you start messing with serialization you will notice that you do receive the CodeDom structure for the entire InitializeComponent method, but unfortunately you may not modify anything except provide your own batch of CodeDom statements. This causes issues since you may not affect the initial statement which creates the actual ComponentResourceManager. What you can do however is generate CodeDom statements which replace the resources variable with your own resources and this is what the LocalizerComponentSerializer does inside its Serialize method. Note: The actual CodeDom statement which *overrides the resource variable has to be ignored within the Deserialize method.*

Now that we have successfully replaced the resource manager in the InitializeComponent designer method we are left with the problem of statements which use the resource manager which have already been executed prior to our component code. Remember that I’ve said that you may not change the code of other serialized components in the designer method? Visual Studio designer serializes components/controls as it finds them inside its component collection which is ordered in the way that user was placing components/controls on the designer surface. This means that if our LocalizerComponent was placed last it will also get serialized last and this means that we will override the default resource manager after it has already been used to provide text to other controls.

In order to make our component come first in serialization process we have to ensure that it is the first component in the component collection of the designer. Unfortunately our serializer does not help us there because it is already late when it comes into play. What we can do is to provide our own component/control designer via the DesignerAttribute attribute attached to our class. For LocalizerComponent I’ve created designer just for that purpose called LocalizerComponentDesigner. This designer does two things: – It ensures that when our LocalizerComponent is dragged onto the designer surface it gets inserted as first component in the component collection (actually as second since the actual Form/UserControl being designed is the first component) – It also sets the Localizable property of the root control (Form/UserControl)

With both LocalizerComponentSerializer and LocalizerComponentDesigner our LocalizerComponent is pretty much done. In order to use it the developer just needs to drag it onto the designer surface and thats it …. well there is still one place that it fails. Although the component is now placed as first component in the InitializeComponent method there is still code which executes before it and that is the actual root component of the designer. For a standard .NET Form control this is not a problem since the control itself has just a few properties affected by resource manager and they do get set at the end of the InitializeComponent method. But if you have a custom Forms (or custom UserControl) derived class which contains additional child items then these items get serialized right at the beginning of the designer generated method. One example of such a class is the DevExpress RibbonForm where the ribbon and all of its items get serialized into designer method before any of child components do.
It seems like there is not much you can do here because you can not affect that code … but you can read it … and then repeat it :). This is a bit of hack and it might not work for all such root classes but it does provide a solid workaround. In essence the LocalizerComponentSerializer not only serves to override the default resource manager but also to scan any previous statements for any calls of resource manager methods (such as GetResource and ApplyResources and repeats them again but now with replaced resource manager. With this we are finished with our localizer component.

Loading/Saving translations

The localizer component itself does not contain or load/save translations. It uses another class called Localizer. Localizer class is a translation manager. It holds the actual list of translations and performs saving and loading of them. This however again it does not do by itself but rather through a class deriving from ITranslationProvider. There are two translation providers provided in the library SqlTranslationProvider (which uses MS SQL database) and SqlCeTranslationProvider which uses CE database.
So in order to use the localizer library in a project we should create/use a database of appropriate type and create a table which holds our translations. Although the table name and the name of the table fields can be customized for each provider the defaults are the following:

Translations
– Key nvarchar(1000)
– LanguageID int
– Text nvarchar(1000)
– UserDefined bit

More information on how to create table and how it is filled in in the Codeplex project site. Check out the sample demo application to see how this is used.

Creating translate base class

In order to make it easy for the developer to work with our localizer component it would be nice if we could provide base class deriving from Form class so that the developer just needs to inherit from it and everything just works. Now even though it might look that it should be sufficient to just create Form derived class, drag the localizer component to it and call it base class it would not work. The problem here is that each type on its own is localizable. Meaning that if you have hierarchy such as: – Form -> MyBaseForm -> MyWorkerForm each one of those classes has its own resource manager and resource file (assuming Localizable == true for each one of them). If you want our localizer component to affect all of them you have to drag a new instance of it to each one of them.
Does this mean that we can not make base class? No, it just means we have to be a bit more clever.

Inside the library there is a class called FormTranslatable which inherits from default .NET Form which can be used as base class for each form that you want to localized/translated. This class uses the fact that the component being designed (root component in the designer) is sited in the designer and that it can access the designer and its services during design phase. At that point when it detects that it is sited in the designer and there is no LocalizerComponent already in the designer component collection it just adds a new instance of LocalizerComponent.

End user translation management

Besides having the ability to perform our own resource translations via database or similar storage system in some scenarios it would also be nice if we could provide the end user of our application to provide translations by him/herself if needed. With that in mind the FormTranslatable base class also provides a way for the end user to perform the actual translation. It installs a new menu item called ‘NLS’ in the ‘Systems’ menu of the form. The systems menu is the menu which appears when you left click on the form icon. This is the only menu which almost always appears on all forms so it is a pretty nice place to invoke localization from. When the user clicks on the new ‘NLS’ menu item a new form pops out (dlgTranslationManagement) where the user can see all the text used inside the form and perform translations.

The interesting thing about end user translation is how the FormTranslatable knows which text it needs to provide for translation. The real form actual type might be several types derived from the FormTranslatable, each one of those types having their own localizer component. The real form might also contain several user controls which contain their own localizer, which in turn might contain other user controls and so on.
The FormTranslatable solves this by reflection. It goes over itself and all of its children (and their children) and gets all content of all class fields (public or private) which contain our localizer component. It aggregates all of those components and passes it to translation form.

Tagged with: , , , ,

Right-click to add watermark/copyright to your image

My wife is making hand-crafted jewellery, bags and similar things (you can see her site Čarapica). One of the problems that we had was that when she placed the images of the things that she created on the internet it would sometimes be ‘borrowed’ by someone and used for some other purposes. In order to prevent that from happening we decided to add watermark to all images that we will be placing on her web site/facebook/online shop.

There might be some existing tools for doing something like that but since I’m a programmer I wanted to created such a utility by myself. Luckily enough it turned out I did not need to do any programming since there are tools such as ImageMagick which can be used for such a purpose. I’ve managed to make a working solution by using similar steps that I used when adding functionality to right click on file to add it to windows firewall.

There are several steps to achieve the desired functionality:

Download the ImageMagick package

Its best that you just download the zip package (you can find the version I used here and unpack it to the folder where your ‘application’ will be located. In my case I downloaded it into c:\Apps\Watermark.

Create a batch file that will perform the actual watermarking of an image

Here is the content of the watermark.bat file which will be used to re-size the image:

c:\Apps\Watermark\convert -size 550x250 -background none -fill #FFFFFF50 -font "Segoe-UI" -pointsize 96 -rotate 25 label:%1 miff:- | c:\Apps\Watermark\composite -tile - "%~n2%~x2" miff:- | c:\Apps\Watermark\convert - -resize 1024x768 "%~n2_w%~x2"

The batch file does several things:

  1. It uses the convert.exe utility to generate a label image which is slightly rotated and semitransparent (text defined by first parameter of the batch file)
  2. It pipes the newly created image to composite.exe utility which takes the original image (defined by seconds parameter of the batch file) and adds the label image on top it
  3. The watermarked image is finally resized to 1024×768 and saved as original_w.extension. This final step can be omitted if you wish to resize the image – just remove the part miff:- | c:\Apps\Watermark\convert - -resize 1024x768 from the command

Here is an example of using the command c:\Apps>c:\Apps\Watermark\watermark.bat "OP" a_view_from_Gradec.jpg on an image from Zagreb (the city where I live):
[onehalf half=”1″]Original image: Zagreb [/onehalf][onehalf half=”2″]Final image: Watermarked [/onehalf]

Enable right click on file to invoke our new batch file

This is relatively simple. We just need to enter a registry entry which will add a new option when you right click on the file. Inside the registry entry you need to define the actual text that will be used as a watermark. Here is the content of the water-register.reg file which can be used to add necessary registry entry:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\*\shell\Watermark\command]
@="cmd.exe /c c:\\Apps\\Watermark\\watermark.bat \"-your watermark text-\" \"%1\""

The drawback of the given registry file is that the right click context menu appears for every file type, not just executable. If somebody know how to make it appear for just image file then please send me a note (and no, just changing the registry from HKEY_CLASSES_ROOT\* to HKEY_CLASSES_ROOT\.jpg or something similar does not do it :)).

Tagged with: ,

DNS issues with Windows 7

Note: I have transferred this blog from my old blog site: http://dcarapic.blogspot.com/ so I am reposting most of the stuff from there

Recently I’ve had a problem with my laptop connected to my home network. For some reason DNS was working intermittently .. meaning that it sometimes worked and sometimes didn’t. It also seamed that it was susceptible to throttling .. meaning that if I opened several browser windows at once it would not work. I’ve scoured the net searching for a solution and was not able to find anything that applied to me. Most of the issues were connected to some 3rd party VPN software or similar.

What really annoyed me was that it was only happening with my laptop and only at home. Finally I found a forum comment somewhere (lost the link unfortunately) where the author stated that turning of IP flood protection on his router fixed the issue for him/her. Reading that I remembered that my router was reset lately (I don’t know how, it just happened) so I had to reconfigure it again. During my configuration I turned on IP flood detection (it looks like something you would want to have turned on). So I opened the router configuration and turned off IP flood detection and lo and behold … everything works great.

How I tested my DNS

Initially I downloaded a small tool called DNS Name Speed Benchmark to determine if there was an issue with my DNS provider but I did not get that much information out of it. It either and all DNS providers could be reached and checked or it did not work at all. After that I just used nslookup command in combination with powershell to run the command in loops.

PS C:\Users\dcarapic> for(1..100) { nslookup www.google.com }

This would execute DNS lookup for the given host (using your default DNS server) a hundred times. During my initial run lookup was time outing here and there and once I tried opening the browser and loading lots of pages it was time-outing all the time. Once I removed the IP flood detection the output of the nslookup was clear and without any time-outs at any time!

Tagged with:

Right click to block programs in windows firewall

Note: I have transferred this blog from my old blog site: http://dcarapic.blogspot.com/ so I am reposting most of the stuff from there

I am using the standard windows firewall and in several cases I wanted to quickly block an application from accessing the internet. Usually this would require me to start up the standard windows firewall configuration utility %windir%\system32\WF.msc and manually add outgoing and/or incoming rules for the application main executable.
This can be a bit tedious so I’ve been searching for an utility to quickly add rules to the windows firewall. Unfortunately I’ve not been successful so I’ve decided to create an utility on my own. My goal was to get a new entry in the right click context menu for an application fiel which would add the blocking firewall rules (in/out). Luckily enough I did not need to do any programming since the required functionality could be achieved by standard windows batch scripting and utilities.

There are 3 parts to achieve the desired functionality:

  1. Creating a batch file that will block the application

Windows 7 already provides a command line utility (netsh.exe) to achieve this and the syntax is the following:

netsh advfirewall firewall add rule name="rule name" dir=in action=block program="some.exe"
netsh advfirewall firewall add rule name="rule name" dir=out action=block program="some.exe"

With this command it is simple to create a batch file which will block the given application executable, lets call it block.bat:

rem block.bat
@set rulename =
@set /p rulename= Rule name (%~n1):
@if '%rulename%' == '' set rulename=%~n1
netsh advfirewall firewall add rule name="%rulename% (in)" dir=in action=block program=%1
netsh advfirewall firewall add rule name="%rulename% (out)" dir=out action=block program=%1

The batch file is simple enough. It is meant to be called with the full path and file name of the executable: block.bat c:\apps\some.exe

After it starts up it asks the user for the name of the rule. If the user just presses ENTER then the name of the executable will be used as a prefix of the name for two rules (incoming and outgoing). So in our case the actual executed commands (if user were to just press ENTER) would be:

netsh advfirewall firewall add rule name="some (in)" dir=in action=block program="c:\apps\some.exe"
netsh advfirewall firewall add rule name="some (out)" dir=out action=block program="c:\apps\some.exe"
  1. Elevating the execution of the batch file

Unfortunately (or fortunately) it is not possible to invoke netsh as a normal user so we need to elevate the execution of the batch file. It should be possible to do this via the standard windows runas command but at home my user does not have a password and runas is not possible if you do not have the password set. Happily enough there are 3rd party utilities which can be used for elevation and the one I use – elevate has the same command line options and cmd.exe so it fits just fine.

  1. Enabling right click on file to invoke our new batch file

This is relatively simple. We just need to enter a registry entry which will add a new option when you right click on the file. New option will invoke the batch file in elevated mode. Batch file will start and asks us for the name of the rule and thats it. Here is the content of the .reg file which can be used to add necessary registry entry (lets call it fw-register.reg:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\*\shell\Firewall (block)\command]
@="c:\\folder\\elevate.exe /c c:\\folder\\block.bat \"%1\""

This registry file assumes that you have the batch file and the elevate utility in c:\folder. Replace c:\\folder with the path to the actual files (do not forget to escape the folder separator \ characters with \\.

The drawback of the given registry file is that the right click context menu appears for every file type, not just executable. If somebody know how to make it appear for just .exe and .com files then please send me a note (and no, just changing the registry from HKEY_CLASSES_ROOT\* to HKEY_CLASSES_ROOT\.exe does not do it :)).

Tagged with:

Using full observer pattern instead of events

Note: I have transferred this blog from my old blog site: http://dcarapic.blogspot.com/ so I am reposting most of the stuff from there

Wikipedia (at the time of this writing) defines Observer pattern as following:
The Observer pattern (a subset of the asynchronous publish/subscribe pattern) is a software design pattern in which an object, called the subject, maintains a list of its dependents, called Observers, and notifies them automatically of any state changes, usually by calling one of their methods. It is mainly used to implement distributed event handling systems.

MSDN defines Events (I refer to it later as ‘eventing system’) as:
Events enable a class or object to notify other classes or objects when something of interest occurs. The class that sends (or raises) the event is called the publisher and the classes that receive (or handle) the event are called subscribers.

.NET eventing system is nothing else then an implementation of an Observer pattern. The subject (type that defines an event) raises the event to notify the Observer (class which adds a handler to the event) that something has occurred.
Having the Observer pattern integrated into the language is a great thing which enables the developer to easily add notifications about object changes. However there are cases where this kind of a system is not sufficient and may lead to poorly designed code if you use it.

The .NET eventing system has the following drawbacks:

  • You must define each ‘change’ as a separate event. Although there are some events which go around this by trying to be ‘generic’ (such as INotifyPropertyChanged.PropertyChanged) they are usually not elegant and seem more like a workaround.
  • You should define each event as EventHandler (or EventHandler<>). This is not mandatory as CLR allows you to define events of any method signature but if you plan on being CLS compliant and not getting compiler warnings then you usually should follow this convention.

In most cases these drawbacks are not serious and they do not affect your code quality that much. But there are some cases (especially when you have objects where there are lots of changes of various types) where the eventing system is just not good enough. For such cases you should build you own custom Observer pattern.

Building you own Observer pattern is quite simple and can be expressed in these few steps:

  1. Select the type which will be the subject (lets call it Subject)
  2. Define the changes that this type will expose
  3. Create a new interface which will define methods that represent the changes of the Subject (for example ISubjectObserver)
  4. Create a new interface method for each change (for example OnSubjectNameChanged, OnSubjectTypeChanged, OnSubjectDetailLineAdded etc.) with parameters which are suitable for each notification (old value, new value, added detail object etc.)
  5. Create a new type which will represent the Observer (lets call it Observer)
  6. Have it implement the new interface (public class Observer: ISubjectObserver)
  7. Implement the methods of the interface
  8. Add two methods to the Subject type which will be used to register/unregister an Observer (RegisterSubjectObserver(ISubject**Observer**),UnRegisterSubjectObserver(ISubjectObserver*)`

Looks simple, and it is. If you are reading this you are probably thinking “hey, I know how to do all this”. Well that might be true but developers sometimes forget to think about alternate ways to do something and this might give you another way of looking at things when designing your applications.

Tagged with: ,

Generic PL/SQL procedure to update target table data with source table data

Note: I have transferred this blog from my old blog site: http://dcarapic.blogspot.com/ so I am reposting most of the stuff from there

Sometimes you might need to update data inside a table with fresh data from another table which has the same structure (but it might be in another schema). Usually this might occur if you have some sort of a ‘master’ schema where the ‘global’ data is located and you want to periodically transfer this data to some the same table in the local user schema.

Although it is relatively easy to create an update procedure/PLSQL block to do this type of thing, it is very tedious to do this every time for each table that you want to synchronize in this way The following procedure takes the name of the table and creates dynamic SQL which it then executes via EXECUTE IMMEDIATE. There are some restrictions in order for this to work:

  • Table must have a unique key
  • User must be able to query ‘all_tab_cols’ view
  • User must be able to execute selects against the source table (this one is understandable)

Here is the procedure:

procedure copy_from_source
(
      p_table_name in varchar2
)
as
  cursor cur_columns(c_v_table_name varchar2, c_v_source varchar2) is
    select column_name from all_tab_cols 
    where
    table_name = c_v_table_name and owner = c_v_source and column_id is not null;

  v_id_col varchar2(38) := 'ID';
  v_source_schema varchar2(38) := 'SOURCE';
  v_target_schema varchar2(38) := user;
  v_query varchar2(10000);
  v_columns varchar2(10000):= null;
  v_start number := 0;
begin     
  v_query := null;
  for v_col in cur_columns(p_table_name, v_source_schema) loop
      if v_query is null then
         v_query := 'update ' || v_target_schema || '.' || p_table_name || 
            ' l set ' || chr(13) || chr(10);

         v_query := v_query || '  ' || '"' || v_col.column_name || '"' || ' = (';
         v_query := v_query || 'select ' || '"' || v_col.column_name || '"' || 
            ' from ' || v_source_schema || '.' || p_table_name || ' g where g.' || 
            v_id_col || ' = l.' || v_id_col;

         v_query := v_query || ')';
      else
         v_query := v_query || ', ' || chr(13) || chr(10);
         v_query := v_query || '  ' || '"' || v_col.column_name || '"' || ' = (';
         v_query := v_query || 'select ' || '"' || v_col.column_name || '"' ||
            ' from ' || v_source_schema || '.' || p_table_name || ' g where g.' || 
            v_id_col || ' = l.' || v_id_col;

         v_query := v_query || ')';
      end if;
  end loop;      
  v_query := v_query || chr(13) || chr(10) || ' where ' || v_id_col || ' in ' || 
    chr(13) || chr(10);

  v_query := v_query || ' (select ' || v_id_col || ' from ' || v_source_schema || 
    '.' || p_table_name || ')';

  v_query := v_query || chr(13) || chr(10);
  -- dbms_output.putline(v_query);
  execute immediate v_query;

  v_query := null;
  for v_col in cur_columns(p_table_name, v_source_schema) loop
      if v_columns is null then
         v_columns := v_columns || '"' || v_col.column_name || '"';
      else
         v_columns := v_columns || ', ' || '"' || v_col.column_name || '"';
      end if;
  end loop;
  v_query := 'insert into ' || v_target_schema || '.' || p_table_name || 
    ' (' || v_columns || ')' || chr(13) || chr(10);
  v_query := v_query || ' select '  || v_columns  || ' from ' || v_source_schema || 
    '.' || p_table_name || ' where ' || v_id_col || ' not in ' || chr(13) || chr(10);
  v_query := v_query || ' (select ' || v_id_col || ' from ' || v_target_schema || 
    '.' || p_table_name || ')';
  -- dbms_output.putline(v_query);
  execute immediate v_query;
end;
Tagged with:

Intermittent ORA-12545 error

Note: I have transferred this blog from my old blog site: http://dcarapic.blogspot.com/ so I am reposting most of the stuff from there

Recently I have encountered a strange kind of error while setting up an application on the client computer(s). Application (.NET) connects to an Oracle 11g database (via Microsoft Oracle database provider and Oracle InstantClient 11.0.7.0) which is on a central server. All client computers are on remote locations with a Cisco router and a permanent VPN connection with NAT translation.

On some clients the application would report ORA-12545 error few times while attempting to connect to a database. Once the connection was established the application worked fine. Needless to say I was baffled by the error and could not locate the problem. Finally, after several hours of googling, I have determined that the problem was that we specified the database server with an IP address and there was no DNS name for it.

It would seem that the OCI connected via IP address, and then someone (I am not sure if it is the Cisco router or the Oracle database) returned the real DNS name of the server. This apparently confused the OCI driver which then reported ORA-12545: Connect failed because target host or object does not exist. After adding the real name of the server to the windows\system32\drivers\etc\hosts file the problem was resolved.

Edit: After googling some more I have found this blog post which might shed more light into the problem: http://tardate.blogspot.com/2007/06/check-locallistener-if-you-run-rac.html

Tagged with:
Top